2013-04-09 9 views
1

我輸入一個骯髒的字符串(大量的空格,換行符和多餘的假空格之前標點符號PHP正則表達式 - 去除多餘的換行符 - 字符串操作問題

我的期望輸出的代碼解釋。下方。

看來,我能達到去除多餘的空格+只是標點符號之前刪除空格。但我的輸出仍然具有不需要多餘的換行符。

我用下面,而我的功能從MySQL數據庫打印用戶輸入到屏幕

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數據庫表的用戶輸入,並且在進入數據庫期間我不處理用戶的數據。

回答

2

我發現了簡單但耗時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; 
} 
相關問題