lundi 4 juillet 2016

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:wampwwwprocessfile.php(18)
: regexp code on line 1 Call Stack: 0.0010 143600 1. {main}()
D:wampwwwprocessfile.php:0 0.0350 170328 2. preg_replace()
D:wampwwwprocessfile.php:18 Fatal error: preg_replace(): Failed evaluating 
code:   in D:wampwwwprocessfile.php on line 18 Call Stack: 0.0010 143600
1. {main}() D:wampwwwprocessfile.php:0 0.0350 170328 2. preg_replace()
D:wampwwwprocessfile.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?

Aucun commentaire:

Enregistrer un commentaire