【问题标题】:PHP REGEX - removal of excess line breaks - String Manipulation IssuePHP REGEX - 删除多余的换行符 - 字符串操作问题
【发布时间】:2013-04-09 10:26:41
【问题描述】:

我正在输入一个脏字符串(在标点符号之前有很多空格、换行符和额外的假空格。

下面的代码解释了我想要的输出。

似乎我可以删除多余的空格 + 删除标点符号之前的空格。 但我的输出仍然有多余的换行符。

当我将用户输入从 MySQL db 打印到屏幕时,我使用下面的函数。

echo "\t\t".'<p>'.nl2br(convert_str(htmlspecialchars($comment))).'</p>'."\r\n";

我的自定义函数代码如下:

function convert_str ($str)
{
    // remove excess whitespace
    // looks for a one or more spaces and replaces them all with a single space.
    $str = preg_replace('/ +/', ' ', $str);
    // check for instances of more than two line breaks in a row
    // and then change them to a total of two line breaks
    //did not worked for me --> preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "\n\n", $str);
    $str = preg_replace('/[ \t]+/', ' ', preg_replace('/\s*$^\s*/m', "\n", $str));
    // if exists; remove 1 space character just before punctuations below:
    // $punc = array('.',',',';',':','...','?','!','-','—','/','\\','“','”','‘','’','"','\'','(',')','[',']','’','{','}','*','&','#','^','<','>','|');
    $punc = array(' .',' ,',' ;',' :',' ...',' ?',' !',' -',' —',' /',' \\',' “',' ”',' ‘',' ’',' "',' \'',' (',' )',' [',' ]',' ’',' {',' }',' *',' &',' #',' ^',' <',' >',' |');
    $replace = array('.',',',';',':','...','?','!','-','—','/','\\','“','”','‘','’','"','\'','(',')','[',']','’','{','}','*','&','#','^','<','>','|');
    $str = str_replace($punc,$replace,$str);
    return $str;
}

你能纠正我吗?

更新:我使用准备好的语句将用户输入输入到 MySQL db 表中,并且在进入 db 期间我不操作用户的数据。

【问题讨论】:

    标签: php regex string preg-replace line-breaks


    【解决方案1】:

    我发现了一个简单但耗时 5 小时的原因:只使用 \n 而不是 \r\n

    所以满足我要求的代码是:

    function convert_str ($str)
    {
        // remove excess whitespace
        // looks for a one or more spaces and replaces them all with a single space.
        $str = preg_replace('/ +/', ' ', $str);
        // check for instances of more than two line breaks in a row
        // and then change them to a total of two line breaks
        $str = preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "\r\n\r\n", $str);
        // if exists; remove 1 space character just before punctuations below:
        // $punc = array('.',',',';',':','...','?','!','-','—','/','\\','“','”','‘','’','"','\'','(',')','[',']','’','{','}','*','&','#','^','<','>','|');
        $punc = array(' .',' ,',' ;',' :',' ...',' ?',' !',' -',' —',' /',' \\',' “',' ”',' ‘',' ’',' "',' \'',' (',' )',' [',' ]',' ’',' {',' }',' *',' &',' #',' ^',' <',' >',' |');
        $replace = array('.',',',';',':','...','?','!','-','—','/','\\','“','”','‘','’','"','\'','(',')','[',']','’','{','}','*','&','#','^','<','>','|');
        $str = str_replace($punc,$replace,$str);
        return $str;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 2013-10-13
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多