3

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

6
  • I haven't got the slightest clue what you are asking. Could you give a real world example? Commented Nov 14, 2010 at 15:02
  • @lonesomeday: It's pretty clear to me. Commented Nov 14, 2010 at 15:05
  • @Vincent Glad to hear it! I get the basic point, but I don't understand the example, which is pretty much gobbledegook -- what's the purpose of this escaping? Commented Nov 14, 2010 at 15:07
  • @lonesomeday: So that the echo works correctly. He probably generated this code and want to fix it. Commented Nov 14, 2010 at 15:09
  • @Vincent The echo will work correctly, but this still won't be valid PHP. Commented Nov 14, 2010 at 15:11

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.