删除HTML和特殊字符

时间:2011-08-20 01:03:22

标签: php

我想使用任何PHP函数或其他什么,以便我可以删除任何HTML代码和特殊字符,并只给我字母数字输出

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

我希望输出变为Hello world it s me and love you(只是Aa-Zz-0-9-WhiteSpace)

我尝试了strip_tags,但只删除了HTML代码

$clear = strip_tags($des); echo $clear;

有没有办法做到这一点〜谢谢

9 个答案:

答案 0 :(得分:126)

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

// 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))))));

答案 1 :(得分:13)

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

$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)));

答案 2 :(得分:4)

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

所有这些解决方案也剥离了像ç或à这样的变音符号。

正如PHP documentation中所述,完美的解决方案很简单:

$clear = strip_tags($des);

答案 3 :(得分:1)

以上示例中更详细的方式,以下是您的字符串:

$string = '<div>This..</div> <a>is<a/> <strong>hello</strong> <i>world</i> ! هذا هو مرحبا العالم! !@#$%^&&**(*)<>?:";p[]"/.,\|`~1@#$%^&^&*(()908978867564564534423412313`1`` "Arabic Text نص عربي test 123 و,.m,............ ~~~ ٍ،]ٍْ}~ِ]ٍ}"; ';

代码:

echo preg_replace('/[^A-Za-z0-9 !@#$%^&*().]/u','', strip_tags($string));

Allows:英文字母(大写和小写),0到9和字符!@#$%^&*().

Removes:所有html标签,以及上述

以外的特殊字符

答案 4 :(得分:1)

你可以在一行中完成:) 对GET或POST请求特别有用

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

答案 5 :(得分:1)

这是我一直在使用的功能,我已经从网络上的各种线程中放置了一个删除所有内容,所有标签并为您留下完美短语的功能。有谁知道如何修改此脚本以允许句点(。)?换句话说,将所有内容保留为“原样”,但保留句点或其他标点符号和!还是逗号?让我知道。

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;

}

答案 6 :(得分:0)

允许期间以及任何其他字符只需添加它们:

更改:'#[^a-zA-Z ]#' 到: '#[^a-zA-Z .()!]#'

答案 7 :(得分:0)

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

答案 8 :(得分:0)

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

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