user2799505 user2799505 - 1 year ago 48
PHP Question

insert string which double quotes in tag html

how could insert double quotes in html tag.

php code:

$con = "the teacher said: \"hello\"";

return in php:

'<tagname name="'$con'">'

the problem is that it only takes the text until it finds the first double quote


$con = "the teacher said: &quot;hello&quot;";
echo '<tagname con="'.$con.'">';

Use a . to concentrate strings. Because you can't include a double quote inside an HTML attribute, even when escaping it in PHP, encode the double quote as &quot;, which will show as ". If you want to do this automatically, run

$con = htmlspecialchars($con);

to automatically encode the string. You can run this inline by using

echo '<tagname con="'.htmlspecialchars($con).'">';

PHP manual: htmlspecialchars