简体   繁体   English

在标签html中插入双引号的字符串

[英]insert string which double quotes in tag html

how could insert double quotes in html tag. 如何在html标签中插入双引号。

example: php code: 示例:php代码:

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

return in php: 在php中返回:

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

the problem is that it only takes the text until it finds the first double quote 问题是它只使用文本,直到找到第一个双引号

thanks. 谢谢。

$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; 因为您不能在HTML属性中包含双引号,所以即使在PHP中将其转义,也应将双引号编码为&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 PHP手册: htmlspecialchars

Use htmlspecialchars : 使用htmlspecialchars

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

HTML raw: HTML原始:

<tagname name="the teach said: &quot;hello&quot;">

You have two problems here. 您在这里有两个问题。

  1. You are trying to concatenate strings without a concatenation operator. 您正在尝试不使用串联运算符来串联字符串。 Use . 使用.
  2. You have dealt with the problem of having double quotes in a PHP string that is delimited with double quotes but not with the other problem of having double quotes in an HTML attribute value that is delimited with double quotes. 您已经解决了用双引号定界的PHP字符串中有双引号的问题,但是没有解决了用双引号定界的HTML属性值中有双引号的问题。 Use htmlspecialchars 使用htmlspecialchars

Such: 这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM