如何打印出包含 PHP 标签的原始字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17450960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:54:46  来源:igfitidea点击:

How do I print out raw string which contain tags in PHP?

php

提问by camino

For example:

例如:

$str="<script>alert('hello');</script>";

If I use echo to print it out, it will pop up an alert window in the browser.

如果我使用 echo 打印出来,它会在浏览器中弹出一个警告窗口。

How can I print out the raw string <script>alert('hello');</script>in this case?

<script>alert('hello');</script>在这种情况下如何打印原始字符串?

回答by Zevi Sternlicht

Depends if you want the words scriptin. If yes, then

取决于你是否想要这些词script。如果是,那么

You should use this.

你应该使用这个。

echo htmlspecialchars($str);

See http://php.net/manual/en/function.htmlspecialchars.php

http://php.net/manual/en/function.htmlspecialchars.php

If not just use strip_tagshttp://php.net/manual/en/function.strip-tags.php

如果不只是使用strip_tagshttp://php.net/manual/en/function.strip-tags.php

回答by go-oleg

You can use htmlspecialchars

您可以使用 htmlspecialchars

$str = htmlspecialchars( "<script>alert('hello');</script>" )

docs: http://php.net/manual/en/function.htmlspecialchars.php

文档:http: //php.net/manual/en/function.htmlspecialchars.php