Problem with PHP preg_match

content.tpl

tratata 'hey' tratata <br/>
okay 'aaaaa' <br/>
'trtata' <br/>
echo 'tratata'hmmmm'traatata';
'hello' tratata <br/>

How to change all quotation marks ONLY in echo?

I need

tratata 'hey' tratata <br/>
okay 'aaaaa' <br/>
'trtata' <br/>
echo 'tratata\'hmmmm\'traatata';
'hello' tratata <br/>

thank

+3
source share
1 answer

This is pretty easy with a callback:

$var = preg_replace_callback("`(?<=echo ')(.+)(?=';)`iU", function ($matches) { return addslashes($matches[1]); }, $var)

First, we map the string to echo quotes (and nothing more), then we use the addlashes function on what we found. The ungreedy (U) option is important, therefore. + Does not match the entire line.

+3
source

Source: https://habr.com/ru/post/1774796/


All Articles