Here's a fiddle demonstrating the problem: http://phpfiddle.org/lite/code/hd0t-ebjr
<?php
require "simple_html_dom.php";
$html = file_get_html("https://play.google.com/store/apps/details?id=com.vlambeer.RidiculousFishing&hl=en");
$test = $html->find('.id-app-orig-desc', 0)->innertext;
$data = [
'test' => $test
];
die(var_dump(json_encode($data)));
?>
Scroll down and you'll notice that all the double quotes are not escaped correctly. Although other characters are ("/" for example).
The weird thing is, that I can't seem to reproduce it when copying the test string into the php code. Only when loading it from the url.
Any idea what could be happening here?
These double quotes are not really double quotes. They are just entity html, ", no need to escape it.
Related
I'm encountering a problem involving escaping character that I think it's not simple at all. If this is done in javascript, nothing to say but the context is using echo command (in PHP) to write javascript code like this:
echo "<script>document.getElementById('spanID').innerHTML=\"$x\"</script>";
$x is a variable in PHP environment, which can contain both single and double quotes. What I do here is:
1. Keep the $x not change, and if $x contains any double quote, the above code won't work, the text echoed may look like:
<script>document.getElementById('spanID').innerHTML="leftside"rightside"</script>;
I supposed $x = leftside"rightside, and you can see it surely won't work.
Escape the double quotes in $x (change all " to "), then the text echoed may look like this:
document.getElementById('spanID').innerHTML="leftside"rightside";
The " won't be converted to " when it is assigned to innerHTML attribute of a Span (for e.g), so instead of my want, the innerHTML of my SPAN should be leftside"rightside, it will be leftside"rightside.
If I change the " to ' in the original echo, like this:
echo "<script>document.getElementById('spanID').innerHTML='$x'</script>";
It is the same because $x here can contain both single and double quotes.
I don't find out any other ways to escape quotes in this case. Could you please help me out?
Thanks!
You need to put between the quotes a string that is a valid string of JavaScript containing valid (and safe) HTML.
Your best option is to not use innerHTML and instead use document.createTextNode which means you only need to slash-escape the content.
Otherwise, you need to HTML escape, then slash escape the content. For correctness, your slash-escaping function should escape at least double-quotes, backslashes, and all JavaScript newlines (U+A, U+D, U+2028, U+2029). I believe PHP's addslashes does not handle U+2028 or U+2029 by default but How to escape string from PHP for javascript? has some alternatives.
To put it all together:
$x_escaped = json_encode($x, JSON_HEX_TAG);
echo "<script>document.getElementById('spanID').appendChild(document.createTextNode($x_escaped))</script>"
should do it. The JSON_HEX_TAG makes sure that $x_escaped will not contain </script> or any other content that prematurely ends your script tag. </script> will instead become \u003c/script\u003e.
i can't understand why variables are not working here. Here is the code:
include_once('../simple_html_dom.php');
$url = htmlentities($_GET['q']);
$urlall = str_replace(" ", "+", $url);
$html = file_get_html('http://www.example.com/some?key=$urlall&hl=en');
echo $html->plaintext;
if you look at this code you will found $urlall variable which i have applied in the web address, but this variable can't extract its data. As a new PHP programmer can't understand what to do now to make it works.. Here i have used HTML DOM PARSER..Thanks
Strings inside single quotes are literal, so $urlall is just a string, it won't be replaced with accual variable value. What you want to do is to use double quotes:
$html = file_get_html("http://www.example.com/some?key=$urlall&hl=en");
For futher explanation refer to PHP Strings:
Note: Unlike the double-quoted and heredoc syntaxes, variables and
escape sequences for special characters will not be expanded when they
occur in single quoted strings.
PHP Variables are not parsed and replaced in single quotes.
Try
$html = file_get_html("http://www.example.com/some?key=$urlall&hl=en");
Try to replace single quotes with double:
"http://www.example.com/some?key=$urlall&hl=en"
or use string concatenation instead of direct variable input into string:
'http://www.example.com/some?key='.$urlall.'&hl=en'
Second variant is more preferable.
I hope this helps you
My PHPstorm is throwing a wobbly with the formats of an array. Surprsingly I've found no direct answer to how to have this array formatted. I've tried the following, I'm surprised the single quotes don't work and then the other two but no luck...
$array = array(’$2,000,000’,’$3,000,000’,’$4,000,000’);
$array = array("$2,000,000","$3,000,000","$4,000,000");
$array = array("\$2,000,000","\$3,000,000","\$4,000,000");
The manual doesn't have commas as escapable. Given that the array is for HTML ouput only I could put
$array = array("Ūꯠꯠ","$$3ꯠꯠ","Ŭꯠꯠ");
but i want to LEARN HOW TO DO IT properly!
The single quotes don't work because what you have here are NOT single quotes, but rather curly apostrophes:
// Incorrect - not real single quotes:
$array = array(’$2,000,000’,’$3,000,000’,’$4,000,000’);
// Correct single quotes:
$array = array('$2,000,000','$3,000,000','$4,000,000');
Assuming you may have copy/pasted this from somewhere, always beware curly quotes when working with code. Some CMS' and frameworks will convert them for display purposes, but doing so breaks the code for copy/pasters.
You're using incorrect quotes:
$array = array(’$2,000,000’,’$3,000,000’,’$4,000,000’);
^-- ^-^--- ^---etc....
Those aren't proper quotes, and should be a ' or " character instead.
I have a php string with a lot of information to be displayed inside a textarea html element.
I don't have access to that textarea nor to the script (if any) that generates it.
$somestring = 'first line \nSecond line \nThird line.';
$somestring as NOT been "worked" with trim or filter_var. Nothing.
On the textfield, I get the \n printed on the textarea hence, not interpreted.
What can I try in order to have those new lines applied?
Thanks in advance.
Try wrapping $somestring with " (double quotes) instead of ' (single quotes)
\n, \r and other backslash escape characters only works in double quotes and heredoc. In single quotes and nowdoc (the single quote version of heredoc), they are read as literal \n and \r.
Example:
<?php
echo "Hello\nWorld"; // Two lines: 'Hello' and 'World'
echo 'Hello\nWorld'; // One line: literally 'Hello\nWorld'
echo <<<HEREDOC
Hello\nWorld
HEREDOC; // Same as "Hello\nWorld"
echo <<<'NOWDOC'
Hello\nWorld
NOWDOC; // Same as 'Hello\nWorld' - only works in PHP 5.3.0+
Read more about this behaviour in the PHP manual
EDIT:
The reason single and double quotes behave differently is because they are both needed in different situations.
For instance, if you would have a string with a lot of new lines, you would use double quotes:
echo "This\nstring\nhas\na\nlot\nof\nlines\n";
But if you would use a string with a lot of backslashes, such as a file name (on Windows) or a regular expression, you would use single quotes to simplify it and avoid having unexpected problems by forgetting to escape a backslash:
echo "C:\this\will\not\work"; // Prints a tab instead of \t and a newline instead of \n
echo 'C:\this\would\work'; // Prints the expected string
echo '/regular expression/'; // Best way to write a regular expression
$somestring = "first line \nSecond line \nThird line.";
http://php.net/types.string <-- extremely useful reading
this article is a cornerstone of PHP knowledge and it's just impossible to use PHP without it.
unlike most of manual pages which are are just for quick reference, this very page is one which every developer should learn by heart.
I know this sounds simple, and probably is, but I can't seem ot get this working. Just want to replace all occurences of a double quote with a single quote...tired this but it doesn't work:
$con = str_replace("\"", "'", $content);
Or:
$con = str_replace(chr(34), chr(39), $content);
What you do is correct and should work. If it doesn't, then you may only SEE double quotes, but in reality these are other characters. Possible is html " character rendered as ". There are also several chars very similar to double quotes. hey 'happen' especially when pasting text from word or openoffice. You'll include all possibilities in str_replace (it can take arrays of strings as parameters).
I had the same problem with input from a form.
I used &quot; for my search string and it worked great.
$con = str_replace("&quot;", "'", $content);