繁体   English   中英

如何在使用htmlspecialchars时使用href

[英]How to use a href while using htmlspecialchars

假设我有这个代码

$text = "please visit this <a href="http://example.com">site</a>."
$text = htmlspecialchars($text);
echo $text;

所以please visit this <a href="http://example.com">site</a>. 但我希望它的输出像这样
请访问此网站 有没有办法看看它是否是一个href然后做某事? 谢谢。

只转义URL本身:

$text = 'please visit this <a href="' . htmlspecialchars('http://example.com') . '">site</a>.'

注意单引号和双引号之间的切换。

您的字符串定义很糟糕(正如您可以通过语法高亮显示)。 实际上,您不需要在此实例中转义任何内容,因为您需要功能性HTML。

你可以像这样使用转义字符:

$text = "please visit this <a href=\"http://example.com\">site</a>."
                                   ^- escape here      ^- and here

有时您可以使用不同的引号集,例如:

// use single quote
$text = 'please visit this <a href="http://example.com">site</a>.'

另一种可能是HEREDOC表示法:( stackoverflow无法识别语法)

$text = <<<DELIM
please visit this <a href="http://example.com">site</a>.
DELIM;

暂无
暂无

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

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