8

How can i use string like below code.

$str = 'Is yo"ur name O'reil"ly?';

The above code is just an example..I need to use big html template which contains single and double quotes.I tried Addslashes php method but when i use single and double quote string in that function i get syntax error.Please help me.

Note : my realtime usage is json data like below.

 $string = "
 <html>
 ...
 <b:if cond='data:blog.pageType == "item"'>
 ..
 ";
 $string = '{"method":"template","params":{"1":"'.$string.'"},"token":"12345"}';
asked May 28, 2013 at 13:44

3 Answers 3

10

You can use a heredoc for that:

$string = <<<EOM
 <html>
 ...
 <b:if cond='data:blog.pageType == "item"'>
 ..
EOM;

If you wish to prevent variable interpolation as well you can use nowdoc (since 5.3):

$string = <<<'EOM'
 <html>
 ...
 <b:if cond='data:blog.pageType == "item"'>
 ..
EOM;

Both heredoc and nowdoc have specific formatting requirements, so be sure to read the manual properly.

answered May 28, 2013 at 13:45
Sign up to request clarification or add additional context in comments.

5 Comments

You might also mention that there are no spaces / tabs allowed before the ending of a heredoc.
@Luceos Variable work just fine in heredoc. See this example - php.net/manual/en/language.types.string.php#example-73
Jep, i know; typo; temp's been rising at office xD
@user2425700 that's if you use $variable inside the heredoc, just like inside double quotes, e.g. "hello $variable".
@user2425700 Btw, since you're relatively new to the scene, read the tour section on how to accept answers.
2

I used heredoc to do the same like

$string = <<< EOF
'Is yo"ur name O'reil"ly?'
EOF;
answered May 28, 2013 at 13:47

Comments

1

You can use heredoc like this:

$str = <<< EOF
'Is yo"ur name O'reil"ly?'
EOF;
answered May 28, 2013 at 13:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.