PHP从字符串中删除特殊字符

时间:2011-05-20 14:15:11

标签: php regex string preg-replace

删除特殊字符时遇到问题。我想删除除“()/。% - &”之外的所有特殊字符,因为我将该字符串设置为标题。

我编辑了原始代码(见下文):

preg_replace('/[^a-zA-Z0-9_ -%][().][\/]/s', '', $String);

但是这不能删除特殊字符,例如:“—,“”,“—等等。

原始代码:(这有效但删除了这些字符:“()/。% - &”)

preg_replace('/[^a-zA-Z0-9_ -]/s', '', $String);

8 个答案:

答案 0 :(得分:50)

您的点符合所有字符。逃避它(以及其他特殊字符),如下所示:

preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $String);

答案 1 :(得分:13)

preg_replace('#[^\w()/.%\-&]#',"",$string);

答案 2 :(得分:5)

好好试试!我想你只需做一些小改动:

  • 转义字符类中的方括号([])(也由[]表示)
  • 转义转义字符(\)本身
  • 另外还有一个奇怪的地方-是特殊的:如果它在两个字符之间,则表示一个范围,但如果它在开头或结尾,则表示文字-字符。

你会想要这样的东西:

preg_replace('/[^a-zA-Z0-9_%\[().\]\\/-]/s', '', $String);

如果您想进一步了解此主题,请参阅http://docs.activestate.com/activeperl/5.10/lib/pods/perlrecharclass.html#special_characters_inside_a_bracketed_character_class

答案 3 :(得分:4)

你想要str replace,因为在性能方面, 更便宜并且仍然符合您的需求!

$title = str_replace( array( '\'', '"', ',' , ';', '<', '>' ), ' ', $rawtitle);

(除非这完全是关于安全性和sql注入,在这种情况下,我宁愿选择ALLOWED字符的POSITIVE列表......更好,坚持经过测试,验证的例程。)

顺便说一句,因为OP谈到了标题设置:我不会用任何东西替换特殊的字符,而是用空格替换。与粘在一起的两个单词相比,一个超级空间不是问题......

答案 4 :(得分:2)

<?php
$string = '`~!@#$%^&^&*()_+{}[]|\/;:"< >,.?-<h1>You .</h1><p> text</p>'."'";
$string=strip_tags($string,"");
$string = preg_replace('/[^A-Za-z0-9\s.\s-]/','',$string); 
echo $string = str_replace( array( '-', '.' ), '', $string);
?>

答案 5 :(得分:0)

preg_replace('/[^a-zA-Z0-9_ \-()\/%-&]/s', '', $String);

答案 6 :(得分:0)

mysqli_set_charset($con,"utf8");
$title = ' LEVEL – EXTENDED'; 
$newtitle = preg_replace('/[^(\x20-\x7F)]*/','', $title);     
echo $newtitle;

Result :  LEVEL EXTENDED

通过应用下面的mysql连接代码可以删除许多奇怪的字符。 但在某些情况下,例如删除此类奇怪的字符-您可以在格式上方使用preg_replace。

答案 7 :(得分:-1)

请参阅example

/**
 * nv_get_plaintext()
 *
 * @param mixed $string
 * @return
 */
function nv_get_plaintext( $string, $keep_image = false, $keep_link = false )
{
    // Get image tags
    if( $keep_image )
    {
        if( preg_match_all( "/\<img[^\>]*src=\"([^\"]*)\"[^\>]*\>/is", $string, $match ) )
        {
            foreach( $match[0] as $key => $_m )
            {
                $textimg = '';
                if( strpos( $match[1][$key], 'data:image/png;base64' ) === false )
                {
                    $textimg = " " . $match[1][$key];
                }
                if( preg_match_all( "/\<img[^\>]*alt=\"([^\"]+)\"[^\>]*\>/is", $_m, $m_alt ) )
                {
                    $textimg .= " " . $m_alt[1][0];
                }
                $string = str_replace( $_m, $textimg, $string );
            }
        }
    }

    // Get link tags
    if( $keep_link )
    {
        if( preg_match_all( "/\<a[^\>]*href=\"([^\"]+)\"[^\>]*\>(.*)\<\/a\>/isU", $string, $match ) )
        {
            foreach( $match[0] as $key => $_m )
            {
                $string = str_replace( $_m, $match[1][$key] . " " . $match[2][$key], $string );
            }
        }
    }

    $string = str_replace( ' ', ' ', strip_tags( $string ) );
    return preg_replace( '/[ ]+/', ' ', $string );
}