$txt = <<< EOD
<a href='#'
xx=t
bb=3onmouseover="document.write(
'<scr'+'ipt>alert(\'xxxx\')</scr'+'ipt>'
);">adfasd</a>
EOD;echo preg_replace('/[\r\n ]+/', ' ', $txt);
<a href='#'
xx=t
bb=3onmouseover="document.write(
'<scr'+'ipt>alert(\'xxxx\')</scr'+'ipt>'
);">adfasd</a>
EOD;echo preg_replace('/[\r\n ]+/', ' ', $txt);
xx=t
bb=3onmouseover="document.write(
'<scr'+'ipt>alert(\'xxxx\')</scr'+'ipt>'
);">
注意
我的格式
这里不能变
:(
真难办!</a>
把字符串用">"分割,将得到的1,2,3的字符串中的\r\n替换掉,4不变,然后再拼接起来。
$txt = <<< EOD
<a href='#'
xx=t
bb=3onmouseover="document.write(
'<scr'+'ipt>alert(\'xxxx\')</scr'+'ipt>'
);">
注意
我的格式
这里不能变
:(
真难办!</a>
EOD;//第一步,特殊字符转换:
$txt2 = htmlspecialchars($txt);
$txt2 = str_replace("\r\n","\n",$txt2);//第二步,字符串提取
$string_array = array();
function quote2code($quote){
global $string_array;
static $i=-1;
$i++;
$string_array[$i] = $quote;
return '<'.$i.'>'; //注意,$txt2 中是不会含有 <> 号了
}
$txt2 = preg_replace("/(("(.*?[^\\\\])??")|(\'(.*?[^\\\\])??\'))/mise","quote2code('\\1')",$txt2);
//第三步,标签内换行符替换
function nl2space($code){
global $string_array;
$code = preg_replace('/\n/mis'," ",$code);
$code = preg_replace('/<(\d+)>/mise',"\$string_array[\\1]",$code); //字符串取回
$code = str_replace("\n"," ",$code); //字符串中的换行符替换
return $code;
}
$txt2 = preg_replace('/(<.*?>)/mise',"nl2space('\\1')",$txt2);//第四步,其余字符串取回
$txt2 = preg_replace('/<(\d+)>/mise',"\$string_array[\\1]",$txt2); //字符串取回//第五步,特殊字符换回
$txt2 = str_replace(">",">",$txt2);
$txt2 = str_replace("<","<",$txt2);
$txt2 = str_replace(""","\"",$txt2);
$txt2 = str_replace("&","&",$txt2);
//OK:
echo "<xmp>$txt2</xmp>";?>
<a href='#'
xx=t
bb=3onmouseover="document.write(
'<scr'+'ipt>alert(\'xxxx\')</scr'+'ipt>'
);">adfasd</a>
EOD;preg_match("/(<.+>)(.+)(<\/a>)/s", $str, $matches);
$matches[1] = preg_replace("/\r\n/", " ", $matches[1]);
array_shift($matches);echo implode("", $matches);