I've just tested 3 ways to replace constants in my config file :
// No check
function replaceConstantsNoCheck($value)
{
foreach (array_keys(get_defined_constants()) as $constant)
$value = str_replace($constant, constant($constant), $value);
return $value;
}
// Check with strstr
function replaceConstantsStrstr($value)
{
foreach (array_keys(get_defined_constants()) as $constant)
if (strstr($value, $constant))
$value = str_replace($constant, constant($constant), $value);
return $value;
}
// Check with strpos
function replaceConstantsStrpos($value)
{
foreach (array_keys(get_defined_constants()) as $constant)
if (strpos($value, $constant) !== false)
$value = str_replace($constant, constant($constant), $value);
return $value;
}
Some measurements :
/*
No check : 0.0078179836273193
Strstr : 0.0034809112548828
Strpos : 0.0034389495849609
No check : 0.0067379474639893
Strstr : 0.0034348964691162
Strpos : 0.0034480094909668
No check : 0.0064759254455566
Strstr : 0.0031521320343018
Strpos : 0.0032868385314941
No check : 0.0068850517272949
Strstr : 0.003389835357666
Strpos : 0.0031671524047852
No check : 0.006864070892334
Strstr : 0.0032939910888672
Strpos : 0.0032010078430176
*/
No check method used a least double of the time in all my tests !
It seems to not have significant difference between strstr
and strpos
methods.