php怎么将字符串转为html

王林
王林 原创
2023-03-04 14:52:01 2989浏览

php将字符串转为html的方法:可以利用htmlentities()函数来实现。该函数可以把字符转换为HTML实体,函数语法:【htmlentities(string,quotestyle,character-set)】。

htmlentities() 函数把字符转换为 HTML 实体。

(推荐教程:php图文教程

语法:

htmlentities(string,quotestyle,character-set)

实现代码:

<html>
<body>
<?php
$str = "John & 'Adams'";
echo htmlentities($str, ENT_COMPAT);
echo "<br />";
echo htmlentities($str, ENT_QUOTES);
echo "<br />";
echo htmlentities($str, ENT_NOQUOTES);
?>
</body>
</html>

(视频教程推荐:php视频教程

输出:

John & 'Adams'
John & 'Adams'
John & 'Adams'

如果在浏览器中查看源代码,会看到这些 HTML:

<html>
<body>
John &amp; 'Adams'<br />
John &amp; &#039;Adams&#039;<br />
John &amp; 'Adams'
</body>
</html>

以上就是php怎么将字符串转为html的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。