$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);

解决方案 »

  1.   

    to xuzuning(唠叨)如果<a>asdfa</a>中间有换行的话就破坏了,要中间的不破坏。需要js和php两种正则。js里不能用对像写
      

  2.   

    <a href='#'
    xx=t
    bb=3onmouseover="document.write(
    '<scr'+'ipt>alert(\'xxxx\')</scr'+'ipt>'
    );">
    注意
    我的格式
    这里不能变
    :(
    真难办!</a>
      

  3.   

    搞不懂这和正则有什么关系
    把字符串用">"分割,将得到的1,2,3的字符串中的\r\n替换掉,4不变,然后再拼接起来。
      

  4.   

    zeroleonhart(莱昂哈特):他的 <a ..> 都含有 ">" 号-_-! ...
      

  5.   

    有点复杂,给大家学习学习:)<?
    $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("/((&quot;(.*?[^\\\\])??&quot;)|(\'(.*?[^\\\\])??\'))/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","&#13;&#10;",$code); //字符串中的换行符替换

    return $code;
    }
    $txt2 = preg_replace('/(&lt;.*?&gt;)/mise',"nl2space('\\1')",$txt2);//第四步,其余字符串取回
    $txt2 = preg_replace('/<(\d+)>/mise',"\$string_array[\\1]",$txt2); //字符串取回//第五步,特殊字符换回
    $txt2 = str_replace("&gt;",">",$txt2);
    $txt2 = str_replace("&lt;","<",$txt2);
    $txt2 = str_replace("&quot;","\"",$txt2);
    $txt2 = str_replace("&amp;","&",$txt2);
    //OK:
    echo "<xmp>$txt2</xmp>";?>
      

  6.   

    $str = <<< EOD
    <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);
      

  7.   

    to faisun(暖阳) :我看见了,所以字符串分割后会有4个子字符串而链接字体在第四个子字符串里,所以只改前三个字字符串,第四个不用改。
      

  8.   

    zeroleonhart(莱昂哈特):假如他<a>里面有 N 个>号呢,N是未知的
      

  9.   

    echo preg_replace("/\r\n(?=.*?>[^<>]+?<\/a>$)/is"," $1",$str);