我正在输入一个脏字符串(大量空格,换行符和标点符号前额外的假空格)
我想要的输出在下面的代码中解释。
似乎我可以实现删除多余的空格+来删除标点符号之前的空格。但是我的输出仍然有多余的换行符
当我从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;
}
你能纠正我吗?
我发现了一个简单但耗时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;
}