Function Ereg_Replace() Is Deprecated - How to Clear This Bug

Function ereg_replace() is deprecated - How to clear this bug?

Switch to preg_replaceDocs and update the expression to use preg syntax (PCRE) instead of ereg syntax (POSIX) where there are differencesDocs (just as it says to do in the manual for ereg_replaceDocs).

Deprecated: Function ereg_replace() is deprecated

The function ereg_replace() is deprecated, that means that it is no longer supported by PHP by a particular reason (can be security or performance reasons, for example).

Instead, use preg_replace(). You also need to replace the regular expression string

[ ]{2,}

to

/[ ]{2,}/

Read more about pattern delimiters.

Replacing deprecated ereg_replace() with preg_replace() did not work

You seem to be missing markers around the regular expression. Try this instead (note the slashes around the pattern).

$string = "This is some text and numbers 12345 and symbols !$%^&";
$new_string = preg_replace("/[^A-Za-z0-9 (),.]/", "", $string);
echo "Old string is: ".$string."<br />New string is: ".$new_string;

You can use any character for the markers as long as the same one is found on both sides. Very useful if your pattern is matching / characters. So this is also valid:

$string = "This is some text and numbers 12345 and symbols !$%^&";
$new_string = preg_replace("~[^A-Za-z0-9 (),.]~", "", $string);
echo "Old string is: ".$string."<br />New string is: ".$new_string;

php - deprecated function ereg_replace() is deprecated in php

ereg_replace deprecated. Use preg_replace like this:

$string = preg_replace('/ \+/', ' ', trim($string));

That is important / \+/ pattern. Space and plus(+)



Related Topics



Leave a reply



Submit