replace all “foo” between two HTML tags using REGEX (PHP code)

前端 未结 3 2012
暗喜
暗喜 2021-01-16 22:12

I want a regex code , to replace all \"foo\" strings to \"bar\" , between the html tags pre>< /pre>

here is an example :

< html>
< p>          


        
3条回答
  •  清酒与你
    2021-01-16 22:53

    $string = '< html>
    < p> blah blah blah foo try foo< /p>
    < pre> foo try foo word foofoo < /pre>
    < /html>';
    $string = preg_replace('/foo(?=(?:.(?!< pre>))*< \/pre>)/Um', 'boo', $string);
    echo $string;
    

提交回复
热议问题