Php 去掉HTML和特殊字符

Php 去掉HTML和特殊字符,php,Php,我想使用任何php函数或任何东西,这样我就可以删除任何HTML代码和特殊字符,只给我字母数字输出 $des = "Hello world)<b> (*&^%$#@! it's me: and; love you.<p>"; 有什么办法吗?去掉标签,只留下字母数字字符和空格: $clear = preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags($des)); 编辑:这一切都归功于DaveRando

我想使用任何php函数或任何东西,这样我就可以删除任何HTML代码和特殊字符,只给我字母数字输出

$des = "Hello world)<b> (*&^%$#@! it's me: and; love you.<p>";

有什么办法吗?

去掉标签,只留下字母数字字符和空格:

$clear = preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags($des));
编辑:这一切都归功于DaveRandom的完美解决方案

$clear = preg_replace('/[^a-zA-Z0-9\s]/', '', strip_tags(html_entity_decode($des)));

在这里用正则表达式替换可能更好

// Strip HTML Tags
$clear = strip_tags($des);
// Clean up things like &amp;
$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))))));

从上面的示例中可以更详细地看到,下面是您的字符串:

$string = '<div>This..</div> <a>is<a/> <strong>hello</strong> <i>world</i> ! هذا هو مرحبا العالم! !@#$%^&&**(*)<>?:";p[]"/.,\|`~1@#$%^&^&*(()908978867564564534423412313`1`` "Arabic Text نص عربي test 123 و,.m,............ ~~~ ٍ،]ٍْ}~ِ]ٍ}"; ';
允许:
英文字母(大写和小写)、0到9和字符
!@$%^&*()。


删除:
所有html标记和除上述字符以外的特殊字符

您可以在一行中完成:) 特别适用于GET或POST请求

$clear = preg_replace('/[^A-Za-z0-9\-]/', '', urldecode($_GET['id']));

这是我一直在使用的一个函数,它是我从网络上的各个线程组合而成的,它删除了所有的东西,所有的标签,给你留下了一个完美的短语。是否有人知道如何修改此脚本以允许句点(.)?换句话说,保持一切“原样”,但不要使用句号或其他标点符号,如和!还是逗号?让我知道

function stripAlpha( $item )

{

    $search     = array( 
         '@<script[^>]*?>.*?</script>@si'   // Strip out javascript 
        ,'@<style[^>]*?>.*?</style>@siU'    // Strip style tags properly 
        ,'@<[\/\!]*?[^<>]*?>@si'            // Strip out HTML tags
        ,'@<![\s\S]*?–[ \t\n\r]*>@'         // Strip multi-line comments including CDATA
        ,'/\s{2,}/'
        ,'/(\s){2,}/'

    );

    $pattern    = array(

         '#[^a-zA-Z ]#'                     // Non alpha characters
        ,'/\s+/'                            // More than one whitespace

    );

    $replace    = array(
         ''
        ,' '

    );

    $item = preg_replace( $search, '', html_entity_decode( $item ) );
    $item = trim( preg_replace( $pattern, $replace, strip_tags( $item ) ) );
    return $item;

}
函数条带($item)
{
$search=数组(
'@]*?>.@si'//去掉javascript
,“@]*?>。@siU'//条形样式标记是否正确
,'@@si'//去掉HTML标记
,'@@'//带多行注释,包括CDATA
,'/\s{2,}/'
,'/(\s){2,}/'
);
$pattern=数组(
“#[^a-zA-Z]#”//非字母字符
,'/\s+/'//多个空格
);
$replace=数组(
''
,' '
);
$item=preg_replace($search',html_entity_decode($item));
$item=修剪(preg_replace($pattern,$replace,strip_tags($item));
返回$item;
}

要允许句点和任何其他字符,只需按如下方式添加它们:

更改:“
”[^a-zA-Z]

到:“
”[^a-zA-Z.!][/code>”

所有其他解决方案都令人毛骨悚然,因为它们来自于一个傲慢地认为英语是世界上唯一语言的人:)

所有这些解决方案也去除了像çorá这样的变音符号

如中所述,完美的解决方案是:

$clear = strip_tags($des);

preg_replace(“/[^a-zA-Z0-9\s]/”,“,$string)
这只用于删除特殊字符,而不是字符串之间的空格。

删除所有特殊字符不要在单行中给空格写

trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', 
urldecode(html_entity_decode(strip_tags($string))))));

非常感谢,它非常有用,工作非常完美,也感谢你一步一步地解释它。我一看到它就笑了:DFatal error:调用未定义函数html_entities_decode(),代码不是用空格代替字母数字,而不是非字母数字吗?也就是说:
/[A-Za-z0-9]/
不是应该颠倒吗?有没有想过在Matt的regex+1中转义
'
,但是你可能应该把
strip\u标记($des)
变成
strip\u标记(html\u entity\u decode($des))
或者您有可能在输出中出现一些错误的
amp
lt
gt
等…非常感谢您的回答,我非常喜欢这个想法。@JackBen:很高兴看到您的回答:)如何让UTF-8字符可以接受,但删除其他特殊字符
$clear = strip_tags($des);
trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', 
urldecode(html_entity_decode(strip_tags($string))))));