Search code examples
phppreg-replacedeprecatedereg-replace

ereg_replace to preg_replace conversion


How can i convert:

ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", " ", $data);

to

preg_replace("......", " ", $data);


Solution

  • Everything you have in the regular expression appears to be valid for a perl regular expression. The difference is that perl regular expressions expect bounding characters (defined by the first character), and optional flags after the second bounding character (which I'll ignore, see the PHP page for preg_match for their use).

    In other words, an ereg match of ^\d$ becomes /^\d$/, where / is the bounding character. If you start with a different character, that becomes the bounding character. This is useful, as in your case, when the usual bounding character of / is used heavily in the regular expression and you don't want to have to escape it. For example, /^\d$/ and |^\d$| are equivalent.

    In this specific case, you can use this as a preg_replace:

    preg_replace("|[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]|", " ", $data);