有现成函数strip_tags看看手册吧

解决方案 »

  1.   

    楼上误会我的意思了,我不是要去除HTML标签,我是要在替换的时候仍然不改动HTML标签
      

  2.   

    <?php$str = '<font color="red">red hat</font>';$str = preg_replace('/(<[^\/>]+>)([^<]+)(<\/[^>]+>)/', "\\1blue hat\\3", $str);print htmlspecialchars($str);?> 输出:<font color="red">blue hat</font>
      

  3.   

    $myString = "<font color=\"red\">red hat</font><aaa color=\"red\">aaa</aaa>";
    $resultString = preg_replace("/(<[^>]*>)[\s\w]+(<\/[^>]*>)/", "\\1你想改的放这里\\2", $myString);
      

  4.   

    $str = "<font color=\"red\">red hat</font>  <aaa color=\"red\">aaa</aaa><img alt='hot'>sdfsdf</img> ";
    echo preg_replace("/(?<=>)\s*\w[^<]+\s*(?=<)/is","BLUE HAT",$str);
      

  5.   

    不好意思,前段时间一直没时间。看来大家还是不懂我的意思。我是要在一个HTML文件里面替换除了HTML标签以外的指定关键字。如这段<font color="red">red hat前段时间一直没时间</font><span id="red">看来大家red 还是不red懂我的意思</span>这段文字要把RED替换成blue,结果应该是这样:<font color="red">blue hat前段时间一直没时间</font><span id="red">看来大家blue 还是不blued懂我的意思</span>请问有没有高手可以解决?
      

  6.   

    $s='<font color="red">red hat前段时间一直没时间</font><span id="red">看来大家red 还是不red懂我的意思</span>';
    preg_match_all("/>([^<]+?)</",$s,$m);
    print_r(array_map("tr",$m[1]));
    function tr($h) {
        return str_replace("red","blue",$h);
    }
      

  7.   

    谢谢Aylazhang,你的方法很接近了!我用你写的代码打出了一下的结果,但是如何把替换之后的字符串接回原来的HTML?是不是需要再写一个正则匹配那些HTML,然后将两个结果的数组拼起来?
    array(2) {
      [0]=>
      array(3) {
        [0]=>
        string(39) ">red   hat前段时间一直没时间 <"
        [1]=>
        string(3) "> <"
        [2]=>
        string(48) ">看来大家red   还是不red懂我的意思 <"
      }
      [1]=>
      array(3) {
        [0]=>
        string(37) "red   hat前段时间一直没时间 "
        [1]=>
        string(1) " "
        [2]=>
        string(46) "看来大家red   还是不red懂我的意思 "
      }
    }
    array(3) {
      [0]=>
      string(38) "blue   hat前段时间一直没时间 "
      [1]=>
      string(1) " "
      [2]=>
      string(48) "看来大家blue   还是不blue懂我的意思 "
    }
      

  8.   

    $s= ' <font   color="red" >red   hat前段时间一直没时间 </font > <span   id="red" >看来大家red   还是不red懂我的意思 </span > ';
    preg_match_all("/>([^<]+?)</is",$s,$m);
    var_dump($m);
    $r = array_map("tr",$m[1]);
    var_dump($r);
    function   tr($h)   {
    return   str_replace("red","blue",$h);
    }preg_match_all("/(<[^>]+?>)(.*?)/is" , $s , $m1);
    var_dump($m1);输出array(2) {
      [0]=>
      array(3) {
        [0]=>
        string(39) ">red   hat前段时间一直没时间 <"
        [1]=>
        string(3) "> <"
        [2]=>
        string(48) ">看来大家red   还是不red懂我的意思 <"
      }
      [1]=>
      array(3) {
        [0]=>
        string(37) "red   hat前段时间一直没时间 "
        [1]=>
        string(1) " "
        [2]=>
        string(46) "看来大家red   还是不red懂我的意思 "
      }
    }
    array(3) {
      [0]=>
      string(38) "blue   hat前段时间一直没时间 "
      [1]=>
      string(1) " "
      [2]=>
      string(48) "看来大家blue   还是不blue懂我的意思 "
    }
    array(3) {
      [0]=>
      array(4) {
        [0]=>
        string(21) "<font   color="red" >"
        [1]=>
        string(8) "</font >"
        [2]=>
        string(18) "<span   id="red" >"
        [3]=>
        string(8) "</span >"
      }
      [1]=>
      array(4) {
        [0]=>
        string(21) "<font   color="red" >"
        [1]=>
        string(8) "</font >"
        [2]=>
        string(18) "<span   id="red" >"
        [3]=>
        string(8) "</span >"
      }
      [2]=>
      array(4) {
        [0]=>
        string(0) ""
        [1]=>
        string(0) ""
        [2]=>
        string(0) ""
        [3]=>
        string(0) ""
      }
    }
    接起来似乎很困难
      

  9.   


    $s= '<font color="red" >red hat前段时间一直没时间 </font ><span id="red">看来大家red   还是不red懂我的意思 </span >';
    echo preg_replace("/(?<!=\")red(?!\")/","BLUE",$s);
    这个不能保证你的tag外面的也有 ="red 这样的字符串。
    比如:
    <font color="red"> sdfsf ="red 一直 </font> 这样的情况。
      

  10.   


    $keyword2 = "<font style='color:red'>$keywords</font>";
    $pattern='/(?!<[^>]*)('.$keywords.')(?![^<]*>)/i';
    $str=preg_replace($pattern,$keyword2,$str);
    $str=preg_replace("@&(\w{0,6})?({$keyword2})(\w{0,6})?;@","&$1$keywords$3;",$str);