php html multiple quotation marks invalid code - php

how can i make sure these quotation marks become valid in PHP?
<?
echo "oaktree.addItem('test1<img src='img.png'>', branch1, '');";
echo "oaktree.addItem('test2<img src='img.png'>', branch1, '');";
?>
the problem is in the tag...
thanks

Your original code is correct as far as PHP syntax is concerned, but it does not output correctly formatted JavaScript, as you are already aware. You can use double quotes inside double quotes in PHP as long as you escape them properly. You can do
<?
echo "oaktree.addItem('test1<img src=\"img.png\">', branch1, '');";
echo "oaktree.addItem('test2<img src=\"img.png\">', branch1, '');";
?>

Try this:
<?php
echo <<<EOT
oaktree.addItem('test1<img src="img.png">', branch1, '');
oaktree.addItem('test2<img src="img.png">', branch1, '');
EOT;
?>

Related

How to output a string containing text surrounded by angle brackets? [duplicate]

I want to print following text as it is:
echo "<label> AAAAA";
But it is just showing 'AAAAA' as output.
How can I escape '<' and '>' symbol.
Use htmlspecialchars.
<?php
echo htmlspecialchars("abc & < >");
?>
<?php
$string = "<label> AAAAA"; //whatever you want
echo htmlspecialchars($string);
?>
refrence htmlspecialchars
echo htmlentities("<label> AAAAA");
Use the htmlentities() function to convert into a plain text string.
<?php
echo htmlentities("<label> AAAAA");
?>
check this http://php.net/manual/en/function.htmlentities.php, and this is code -
echo htmlentities ("<label> AAAAA");
You should escape your especial characters for HTML.
echo "<label> AAAA"
http://www.w3schools.com/tags/ref_entities.asp
echo "<label> AAAAA";
Use HTML entities: < for < and > for >. Could be achieved using htmlspecialchars function: http://php.net/htmlspecialchars.
Read more about HTML entities here: http://www.santagata.us/characters/CharacterEntities.html

How to insert a new line break within the php code

<?php echo nl2br($valueu->getarea($rows['province'].'|'.$rows['city'].'|'.$rows['area'],' ')); ?></td>
how to put a line break in between city and area when outputted to a browser.
Thanks
Use \n. Just add it with the sting like this I."\n" like pie.
You can also use nl2br like this echo nl2br("One line.\nAnother line.");
Try double quote "\n".
If you use single quote '\n', PHP will just interpret as a literal \n, but within double quote, it will parse it as an escape sequence (newline character) and echo it. More about strings here.
echo nl2br('Hello \n world');
// -> Hello \n world
echo nl2br("Hello \n world");
// -> Hello <br /> world
In a browser, you may need to use "<br/>" instead of a newline "\n".
echo($valueu->getarea($rows['province'].'|'.$rows['city'].'<br />'.$rows['area'],' '));
Something like that?

HTML,PHP - Escape '<' and '>' symbols while echoing

I want to print following text as it is:
echo "<label> AAAAA";
But it is just showing 'AAAAA' as output.
How can I escape '<' and '>' symbol.
Use htmlspecialchars.
<?php
echo htmlspecialchars("abc & < >");
?>
<?php
$string = "<label> AAAAA"; //whatever you want
echo htmlspecialchars($string);
?>
refrence htmlspecialchars
echo htmlentities("<label> AAAAA");
Use the htmlentities() function to convert into a plain text string.
<?php
echo htmlentities("<label> AAAAA");
?>
check this http://php.net/manual/en/function.htmlentities.php, and this is code -
echo htmlentities ("<label> AAAAA");
You should escape your especial characters for HTML.
echo "<label> AAAA"
http://www.w3schools.com/tags/ref_entities.asp
echo "<label> AAAAA";
Use HTML entities: < for < and > for >. Could be achieved using htmlspecialchars function: http://php.net/htmlspecialchars.
Read more about HTML entities here: http://www.santagata.us/characters/CharacterEntities.html

PHP Echo syntax error with single quote or double quotes?

The is a very simple echo statement but I can't solve it?
echo '"What is your name?'";
Mismatch of single quotes, use this:
echo '"What is your name?"';
Your first enclosing character was single quote but ending one was double quote causing the problem
Incorrect:
echo '"What is your name?'";
^ Unexpected character
Correct:
echo '"What is your name?';
Correct:
echo "What is your name?";
Correct:
echo 'What is your name?';
Correct:
echo '"What is your name?"';
Correct:
echo "'What is your name?'";
Your quotes are nested incorrectly.
echo "\"What is your name?\"";
This is where your interpreter is choking:
echo '"What is your name?'";
expecting ; not "
echo "What is your name?";
This is Simply the best. No confusion No problem..:)

PHP - stop character encoding auto-convertion

In html source
\xE6\x82\xA0
result is "\xE6\x82\xA0"
but in php
<?php echo "\xE6\x82\xA0"; ?>
result is "悠" (character for \xE6\x82\xA0 )
what can be done to make php echo \xE6\x82\xA0?
If you want to print the actual string \xE6\x82\xA0, simply replace the double quotes with single quotes. Strings in single quotes are not parsed for escape sequences.
<?php echo '\xE6\x82\xA0'; ?>
Try escaping your slashes.
<?php echo "\\xE6\\x82\\xA0"; ?>
or simply use single quotes instead of double quotes
<?php echo '\xE6\x82\xA0'; ?>
or you can simply output directly
?>\xE6\x82\xA0<?php
Escape the slash characters.
<?php echo "\\xE6\\x82\\xA0"; ?>
Or write the string directly in template mode.
?>\xE6\x82\xA0<?php

Categories