PHP: Escaping single quotes in javascript echoed by php? - php

I have an echo in my php page which has a javascript in it.
it looks like this:
echo '<ons-list-item onclick="fn.load('home.html')" tappable>';
I need to escape the single quotes on fn.load('home.html')
so i did:
echo '<ons-list-item onclick="fn.load(/'home.html'/)" tappable>';
But that doesn't really work..
could someone please advise on this?
Thanks in advance.

Try using double quotes around "home.html".
Or you need to escape ", so it won't be interpreted as end of string. Use \ to escape the quotes as you said.

Related

How to use php to output this html code?

How to use php to output this html code?
The html code is this.
Piano Programs
but I want to use php to show this code
$program_name = "piano_programs";
echo "Piano Programs";
but.....doesn't work, any idea ,thanks
Backslashes to escape the quotes:
echo "Piano Programs";
You need to add slashes before onclick's two double quotes and move your single quote to the right side of .php.
echo "Piano Programs";
I like to curly-bracket my variables so that they stand out in my editor and so that they don't get mixed up with the text that immediately follows.

how to not parse double quotes within double quotes

Hello there :) I have a script in PHP that creates a file (with the function "file_put_contents()") , and it will put the contents of a PHP file within the file it just created. Within the PHP file, there are double quotations, so as i try to implement the code that i want to put into this newly made PHP file, it has double quotations, and the way how file_put_contents works is the second part of it uses double quotations as well.
To put it into perspective, this is how it goes: file_put_contents('file.php',"code with "" in it")
so as you can see, the double quotations get in the way of the PHP files double quotes.
My question is, how do i get the text within the quotes to not parse?
Thanks
use \ more info
file_put_contents('file.php',"code with \"\" in it")
or use ' to quote second param
file_put_contents('file.php','code with "" in it')
Wrap the string which contains double quotes in a string literal defined using single quotes.
file_put_contents('file.php','code with "" in it');
"Escaping" is what you're looking for. As always, the manual holds all your answers:
https://secure.php.net/manual/en/language.types.string.php
You just need to escape the double quotes like this : file_put_contents('file.php',"code with \"\" in it")
For more information : http://php.net/manual/en/language.types.string.php
Try using addslashes("codewith dowble quotes") to over come this issue

single qoute syntax printing in php [duplicate]

This question already has answers here:
single quote inside double quote in php
(3 answers)
Closed 9 years ago.
The below outputs
href="javascript:showBed(" a114:1')'
when I want it on the form
href="javascript:showBed('A114:1')"
in order to get javascript to work. I had a look at this site but coudn't get it to work so I gave up. Perhaps you could give me a hint on how the corrent syntax would be?
echo("<a href='javascript:showBed('" . $row['Bed'] ."')' target='main' class='larmlink'>link</a>");
Thanks =)
Your output is not what it would output, but it is how it would be interpreted (HINT: don't look at a parsed DOM tree, look at the source).
echo("<a href='javascript:showBed('" . $row['Bed'] ."')' ...
==>
echo("<a href=\"javascript:showBed('" . $row['Bed'] ."')\" ...
You really should be using the more standard double quotes around HTML element properties. As such, it is probably best to use single quotes in PHP. I would suggest this:
echo('link');
To print the double-quote character, you can escape it by doing \"
echo("<a href=\"javascript:showBed('" . $row['bed'] ."')\" target='main' class='larmlink'>link</a>");
Live demo
When you want to output variable data to JavaScript, it is good to use json_encode() so that all special characters are escaped automatically. The htmlspecialchars() escapes any values for use in the HTML attribute value.
echo '<a href="',
htmlspecialchars('javascript:showBed(' . json_encode($row['Bed']) . ')'),
'" target="main" class="larmlink">link</a>';
Note that I use single quotes for PHP string literals so that PHP doesn't have to search through my string for a variable to replace. You don't have to do this, but I recommend it.
I like to use sprintf (or printf, but sprintf is easier to refactor) for long strings like this so it's easy to see the template:
echo sprintf("<a href='javascript:showBed(\"%s\")' target='main' class='larmlink'>link</a>", $row['Bed']);
I'd also consider using addslashes on the $row['Bed'] variable in case it has quotes in it.
Using the heredoc syntax often makes code with mixed quotes easier to understand:
echo <<<EOD
link
EOD;
As others mentioned, if the value of your $row['Bed'] might contain single or double quotes, you have to escape it with addslashes.
You can use the heredoc syntax to avoid to escape anything:
echo <<<LOD
link
LOD;
Notice that if your variables contains some quotes you must use the addslashes function or str_replace before.
Another good practive is to separate systematically all the html content from php code:
<a href="javascript:showBed('<?php
echo $row['Bed'];
?>')" target="main" class="larmlink">link</a>
try this one:
echo("<a href='javascript:showBed(\"" . $row['Bed'] ."\")' target='main' class='larmlink'>link</a>");

single and double quotes in php variable

How can I combine this code with all single and double quotes as it should be.
I have tried several combinations and I can't make it work.
This one is my last try so please help.
What would be a good approach when working with long strings?
$html .='';
I would move your styles to an external stylesheet to make it shorter, and then just escape the quotes like "\"" for " in the string.
$html .="";
This was not tested because I don't have your code :)
Best solution is to use HEREDOC, which completely eliminates the need for ANY quote escaping at the PHP level:
$html .= <<<EOL
<a href="onclick('\$.ajax({ etc.....
EOL;
Note that you'll still be bound by the quoting needs of whatever language(s) you're embedding in the heredoc. But at least you won't have to worry about causing a PHP syntax error because of unbalanced/unescaped quotes.
I follow the rule of: php strings are encapsulated in single quote, so attributes of html are in double quotes.
Any quote in the attribute must be an escaped single quote \'
so:
$html .='';
You should probably just escape the double-quotes inside the other double-quotes (if that makes sense). :)
$html .='';
That (or something similar) should work.

How to json_encode without escaping slashes?

I'm encoding it like so..
json_encode($array_list, JSON_UNESCAPED_SLASHES)
Ex: \n turns into \\n, \r\n turns into \\r\\n
But, it's still escaping the slashes! What's wrong and how to fix it? Thanks.
I think it is because of single and double quotes, see the examples
$arr = array("\n\r");
echo json_encode($arr,JSON_UNESCAPED_SLASHES); // ["\n\r"]
$arr = array('\n\r');
echo json_encode($arr,JSON_UNESCAPED_SLASHES); //["\\n\\r"]
working example http://codepad.viper-7.com/LvWMhq
If it is a concern when doing any MySQL queries then you can use it like this:
mysql_real_escape_string(json_encode($array))
No need to escape anything in the $array itself before this point, just let mysql_real_escape_string escape the json_encoded string.

Categories