php html标签替换_php自定义函数转换html标签示例

本文介绍了PHP中用于处理HTML标签的两个关键函数:htmlentities和html_entity_decode。通过示例代码展示了如何使用它们进行HTML特殊字符的编码与解码,帮助理解这两个函数在PHP程序设计中的作用。
摘要由CSDN通过智能技术生成

本文实例讲述了php自定义函数转换html标签的方法。分享给大家供大家参考,具体如下:

/*

* Created on 2016-9-29

*

*/

$orig = "I'll \"walk\" the dog now";

$a = htmlentities($orig);

$b = html_entity_decode($a);

echo $a; // I'll "walk" the <b>dog</b> now

echo $b; // I'll "walk" the dog now

// For users prior to PHP 4.3.0 you may do this:

function unhtmlentities($string)

{

// replace numeric entities

$string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);

$string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);

// replace literal entities

$trans_tbl = get_html_translation_table(HTML_ENTITIES);

$trans_tbl = array_flip($trans_tbl);

return strtr($string, $trans_tbl);

}

$c = unhtmlentities($a);

echo $c; // I'll "walk" the dog now

?>

运行结果如下图所示:

1b858699ab6bf2cd563c6d3b752f6d38.png

希望本文所述对大家PHP程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值