note 98602 added to function.ereg-replace
| From: | Joe at osu1 dot php dot net | Date: | Fri, 25 Jun 2010 18:24:30 +0000 |
| Subject: | note 98602 added to function.ereg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170465@lists.php.net to get a copy of this message | ||
Since ereg_replace is now deprecated, many users will be coming to this page, in an attempt to
troubleshoot and fix broken scripts after a PHP upgrade.
ereg_replace is essentially a search and replace function and it follows a pattern like this:
<?php
$input = ereg_replace("find this", "replace it with", $output);
?>
You start out by defining the input variable ($input), then the = ereg_replace portion tells php to
do a search and replace action on the value of $input, then once the value has been modified with
your 'find this' and 'replace with' section, the resulting value is defined as a
variable $output which you can then use in other parts of your script or page.
Lets take a real world example.
There is a e-comm site - http://www.customervideoreviews.com - that has
millions of item records. Each item has a name, that is defined as a variable called $title. Each
$title value has spaces, i.e. "Range Cookers" or "Bosch Dishwashers", but we
want to convert these into a page url that does not contain spaces, but uses the plus character
'+' instead of a space (you can use hyphens '-' or other characters).
If using the deprecated ereg_replace function, it might look something like this:
<?php
$title=ereg_replace(" ", "+", $itemurl);
?>
But to avoid those deprecated errors, we change the function name to preg_replace and add a set of
delimiters, in this case the 2 forward slashes / / where the contents inside that are matched as the
'find this' portion.
<?php
$title=preg_replace("/ /", "+", $itemurl);
?>
It is not absolutely necessary to use / / as the delimiters, if you are modifying directory names
which may naturally contain forward slashes, it could cause it to break because the slashes in the
url or directory would be interpreted as the delimiter, so you could use other obscure characters
i.e. ("# #", "+", $itemurl); instead. This would give an output like:
http://www.customervideoreviews.com/-Range+Cooker
http://www.customervideoreviews.com/-Bosch+Dishwasher
This would strip out the spaces from the value of $title and give you a variable called $itemurl
where all the spaces were replaced with the plus character, so you could potentially use it as a url
like above.
So to summarize: ereg_replace is a deprecated search and replace action where preg_replace can be
used instead, and it follows a pattern as described earlier where it takes the input data, finds a
match based on what you are looking for, replaces it with something else you define, and then gives
you an output data variable that you can use in your script or page. Hopefully by understanding the
process, the php code functions wont seem like voodoo and you could troubleshoot and fix many of the
deprecated errors yourself.
----
Server IP: 92.48.74.199
Probable Submitter: 86.150.69.226
----
Manual Page -- http://www.php.net/manual/en/function.ereg-replace.php
Edit -- https://master.php.net/note/edit/98602
Del: integrated -- https://master.php.net/note/delete/98602/integrated
Del: useless -- https://master.php.net/note/delete/98602/useless
Del: bad code -- https://master.php.net/note/delete/98602/bad+code
Del: spam -- https://master.php.net/note/delete/98602/spam
Del: non-english -- https://master.php.net/note/delete/98602/non-english
Del: in docs -- https://master.php.net/note/delete/98602/in+docs
Del: other reasons-- https://master.php.net/note/delete/98602
Reject -- https://master.php.net/note/reject/98602
Search -- https://master.php.net/manage/user-notes.php