Toaster Toaster - 5 months ago 34
PHP Question

PHP preg_replace escape ampersand in backreference regex

Ok, I've checked and looked, but today my SearchFU is not strong. I am encountering an error with backreferences in some Regex

preg_replace('~$\s*<div>([^<]*?)</div>$\s*~me', '\1', $source);
I could really use a hand with. The expression finds
<div>
tags and returns just the content removing the tag and enclosing linefeeds. ie:

<td>
<div>some value</div>
</td>


becomes

<td>some value</td>


Everything works fine until
<div>&nbsp;</div>
which causes:

Parse error: syntax error, unexpected '&' in D:\wamp\www\processfile.php(18)
: regexp code on line 1 Call Stack: 0.0010 143600 1. {main}()
D:\wamp\www\processfile.php:0 0.0350 170328 2. preg_replace()
D:\wamp\www\processfile.php:18 Fatal error: preg_replace(): Failed evaluating
code: in D:\wamp\www\processfile.php on line 18 Call Stack: 0.0010 143600
1. {main}() D:\wamp\www\processfile.php:0 0.0350 170328 2. preg_replace()
D:\wamp\www\processfile.php:18


If I remove the \1 backreference, the expression will happily erase all the content in the div tags, but seems to want to parse it as a new expression.

Is there a way around this, or should I just pre-process the &'s before this line gets executed?

Answer

It's the e modifier at the end of your regex. It causes the replacement to be eval()'ed. Remove it:

preg_replace('~$\s*<div>([^<]*?)</div>$\s*~m', '\1', $source);

See the PHP docs for PCRE pattern modifiers.