Isis
Isis

Reputation: 4666

PHP preg_match problem

content.tpl

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

How do I change all the quotes ONLY in the echo?

I need

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

Thank you

Upvotes: 3

Views: 196

Answers (1)

Vincent Savard
Vincent Savard

Reputation: 35927

It's pretty easy with a callback :

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

First, we match the echo quoted string (and nothing else), then we apply the addslashes function on what we found. The ungreedy (U) option is important so the .+ doesn't match the whole string.

Upvotes: 3

Related Questions