I'm trying to make a preg_replace()
pattern to convert the text "orderId" into "order-id".
$argumentName = "orderId";
$argumentName = preg_replace("/([A-Z])/e", "-strtolower($1)", $argumentName);
echo $argumentName;
The output for this line is "order0d". Why is this not working?
Since you're specifying that your replacement-string is an expression, this:
strtolower($1)
is 'i'
(as it should be), and this:
-strtolower($1)
is -'i'
("negative 'i'
"), which forces the string 'i'
to be interpreted as a number — 0
.
What you want is
$argumentName = preg_replace("/([A-Z])/e", "'-' . strtolower($1)", $argumentName);
which concatenates the strings '-'
and 'i'
.