faqts : Computers : Programming : Languages : PHP : Common Problems : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

14 of 16 people (88%) answered Yes
Recently 6 of 7 people (86%) answered Yes

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("<", "&lt;", $one);
    eregi_replace(">", "&gt;", $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("<", "&lt;", $one);
    $one = eregi_replace(">", "&gt;", $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.