去除HTML和特殊字符 [英] Strip out HTML and Special Characters
本文介绍了去除HTML和特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用任何php函数或其他任何函数,以便我可以删除任何HTML代码和特殊字符,并且仅提供字母数字输出
I'd like to use any php function or whatever so that i can remove any HTML code and special characters and gives me only alpha-numeric output
$des = "Hello world)<b> (*&^%$#@! it's me: and; love you.<p>";
我希望输出变为Hello world it s me and love you
(只是Aa-Zz-0-9-WhiteSpace)
I want the output become Hello world it s me and love you
(just Aa-Zz-0-9-WhiteSpace)
我已经尝试过strip_tags
,但是它仅删除了HTML代码
I've tried strip_tags
but it removes only HTML codes
$clear = strip_tags($des); echo $clear;
有什么办法做〜谢谢
推荐答案
在这里使用正则表达式替换可能更好
Probably better here for a regex replace
// Strip HTML Tags
$clear = strip_tags($des);
// Clean up things like &
$clear = html_entity_decode($clear);
// Strip out any url-encoded stuff
$clear = urldecode($clear);
// Replace non-AlNum characters with space
$clear = preg_replace('/[^A-Za-z0-9]/', ' ', $clear);
// Replace Multiple spaces with single space
$clear = preg_replace('/ +/', ' ', $clear);
// Trim the string of leading/trailing space
$clear = trim($clear);
或者一口气
$clear = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($des))))));
这篇关于去除HTML和特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文