Escaping $ in HEREDOC string with PHP - php

I have something like this
$var = <<<HEREDOC
..."request": ${"hello"}...
HEREDOC;
Of course PHP thinks ${"hello"} is a variable and it fails to load.
How to escape the $?

The same way you escape almost anything in a string
$var = <<<HEREDOC
..."request": \${"hello"}...
HEREDOC;
echo $var;
RESULT
..."request": ${"hello"}...

Is that what you want?
$var = <<<HEREDOC
..."request": \${"hello"}...
HEREDOC;

Related

PHP heredoc is not working when heredoc's string from MySQL text Type

I store heredoc in MySQL text type like this.
Hi, $candidate.
This is $interviewer speaking.
I want to use heredoc dynamicaly like below.
("$mailTemplate->body" is string from MySQL text type above.)
$candidate = 'CANDIDATE';
$interviewer = 'INTERVIEWER';
$mailBody = <<< EOM
$mailTemplate->body
EOM;
But heredoc is not working, variables are output as they are.
Hi, $candidate. This is $interviewer speaking.
Any idea? Or is it impossible?
Thanks.
No, you cant do it like this. $... in your $mailTemplate->body is treated as text.
But you can use sprintf for this.
$text = "Hello %s this is a %s.";
$valueA = 'World';
$valueB = 'test';
echo sprintf(
$text,
$valueA,
$valueB
);
Working example.
output
Hello World this is a test.
Replace all the vars in your string by %s and provide the values as parametes in sprintf.

Does heredoc output variables which contains quotes?

I am using this:
$variable = "Name is \"Bob\"";
$message = <<<EOF
<input type="text" value="$variable">
EOF;
And the result is :
Actually, this is synthetic example and I am working with db. But I tried: this synthetic example works (to simulate problem, actually it shows that what I am doing is not working).
Yes, the quotes will appear in the HTML.
Since the quotes will end the attribute value, you'll make the HTML invalid.
You need to make the variable HTML-safe with htmlspecialchars().
You are generating invalid HTML:
<input type="text" value="Name is "Bob"">
Please use htmlspecialchars() to encode $variable before insertion.
A heredoc is just a convenient shortcut for a multi-line echo. It doesn't care WHAT'S in the string, it'll just be output.
There is NO difference between the following two constructs:
$foo = "A string with an \" embedded quote";
echo <<<EOL
Hello, $foo, how are you
EOL;
echo "Hello, $foo, how are you";
The only real difference is that you don't have escape quotes in the rest of the string:
echo <<<EOL
This is a "quoted phrase" within a sentence
EOL;
echo "This is a \"quoted phrase\" within a sentence";

PHP : Using single quotes and double quotes in single string

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"}';
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.
I used heredoc to do the same like
$string = <<< EOF
'Is yo"ur name O'reil"ly?'
EOF;
You can use heredoc like this:
$str = <<< EOF
'Is yo"ur name O'reil"ly?'
EOF;

Getting rid of escape characters in PHP

I've got a string containing values such as "hello world\' hello world\'" and I'd like to get rid of the escape characters (the backslashes.)
I've tried the following code:
str_replace("\\", "", $data);
But it doesn't seem to work.
If all you want to do is to get rid of backslashes, then there's a very handy PHP function that accomplishes just that
$var = stripslashes($var);
Assuming you're using $var as the last parameter in str_replace() instead of $data, it should work fine.
$var = "hello world\' hello world\'";
echo $var . "<br />";
echo str_replace("\\", "", $var) . "<br />";
Output:
hello world\' hello world\'
hello world' hello world'
this should work great for you you were not referencing the variable $var correctly in php replace subject parameter also assuming you need to replace the \' you were putting \ which searches for it hence nothing was found to be replaced hope this helps
$var = "hello world\' hello world\'";
echo str_replace("\'","",$var);

How can I store PHP code inside a 'heredoc' variable?

I would like to store the following code inside a HEREDOC variable:
<?php
$var = 'test';
echo $var;
?>
like this:
$hered = <<<HERED
<?php
$var = 'test';
echo $var;
?>
HERED;
The problem is that HEREDOC works like double quotes, "" - that means each dollar sign ($) has to be replaced with \$...
Is there a way to use HEREDOC without performing such an operation?
Yes, there is. Check out the nowdoc syntax:
$hello = 'hey';
$echo <<<'EOS'
$hello world!
EOS;
//Output: $hello world
It can be done using \:
echo <<<HEREDOC
<?php
\$var = 'test';
echo \$var;
?>
HEREDOC;
I know there's now doc, but for example my current use case needs heredoc, because some dollars need to be escaped and some not:
$variableNotToEscape = 'this should be output';
echo <<<HEREDOC
$variableNotToEscape
\$variableNotToEscape
HEREDOC;
Which returns
this should be output
$variableNotToEscape

Categories