Php5 to php7 script help: ereg_replace

Can someone please help me convert this script to make it compatible with php7?
I’ve tried using preg_replace, but I keep getting an error about delimiters.

function replaceTag($str, $tag, $value)
    if ($value == " ") {
        $str = ereg_replace(strtolower($tag), $value, $str);
        $str = ereg_replace(strtoupper($tag), $value, $str);
    $str = ereg_replace(strtolower($tag) . '([^=]*)=([^"]*)"([^"]*)"', $value, $str);
    $str = ereg_replace(strtoupper($tag) . '([^=]*)=([^"]*)"([^"]*)"', $value, $str);
    $str = ereg_replace(strtolower($tag) . "([^=]*)=([^']*)'([^']*)'", $value, $str);
    $str = ereg_replace(strtoupper($tag) . "([^=]*)=([^']*)'([^']*)'", $value, $str);
    return $str;

You need to surround your regular expressions with a delimiter character. You’ll also need to escape any uses of that delimiter in your tag. If your tag is just a string and you don’t expect to use regex characters, you can use preg_quote. In the example below I’ve used a forward slash as a delimiter.

$str = preg_replace('/' . preg_quote(strtolower($tag), '/') . '/', $value, $str);
1 Like

Thanks a lot for your reply!
Would you be able to help me where those delimiters need to be in my code?

Is it something like this?

$str = preg_replace('/([^=])=([^"])”([^"])"/' . preg_quote(strtolower($tag), '/([^=])=([^"])”([^"])"/') . '/([^=])=([^"])”([^"])"/', $value, $str);

Literally the first sentence. The delimiters need to enclose your regex. You need to put one at either end of it.

Thank you so much! It works :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service