Entry
Why isn't eregi_replace() working?
Jul 3rd, 1999 06:09
Fred Isler,
A common error when using eregi_replace() is forgetting to assign the
processed string to a variable. In the following example, we need to
replace the angle brackets with their corresponding HTML representations
so the brackets will show up in the browser:
<?php
$one = "String like this <with stuff in brackets>";
eregi_replace("<", "<", $one);
eregi_replace(">", ">", $one);
echo "$one\n\n"
?>
When we echo $one to the browser, this is all we see:
String like this
The brackets haven't been replaced, so the rest of the string is hidden.
The solution: assign the results of eregi_replace() back to the
original variable:
<?php
$one = "String like this <with stuff in brackets>";
$one = eregi_replace("<", "<", $one);
$one = eregi_replace(">", ">", $one);
echo "$one\n\n"
?>
This time when we echo to the browser, we see this:
String like this <with stuff in brackets>
In the first example, eregi_replace() had nowhere to return the
processed string. In the second example, we returned the process string
back to $one, and the changes were saved.