note 98602 modified in function.ereg-replace by danbrown
| From: | danbrown@php.net | Date: | Fri, 25 Jun 2010 18:27:35 +0000 |
| Subject: | note 98602 modified in function.ereg-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170467@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.example.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.example.com/-Range+Cooker
http://www.example.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.
--was--
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.example.com/-Range+Cooker
http://www.example.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.
http://php.net/manual/en/function.ereg-replace.php