例如在对如何下的html进行替换:<div class="abc">
<a href="#a">go to a</a>
    <img src="images/a.gif" alt="this is image a" />
    <p>
     <span>caption</span>
        <span class="content">application</span>
    </p>
</div>
我希望把所有的字母A全部进行加粗显示,但又不能影响到html的显示结果,正确的结果如下:<div class="abc">
<a href="#a">go to <b>a</b></a>
    <img src="images/a.gif" alt="this is image a" />
    <p>
     <span>c<b>a</b>ption</span>
        <span class="content"><b>a</b>ndroid <b>a</b>pplication</span>
    </p>
</div>

解决方案 »

  1.   


    $str = '<div class="abc">
        <a href="#a">go to a</a>
        <img src="images/a.gif" alt="this is image a" />
        <p>
            <span>caption</span>
            <span class="content">application</span>
        </p>
    </div>
    ';$tempStr = str_replace("<","||<",$str); //在html标签前加上分割符||(分割符根据实际情况更改)
    $tempStr = strip_tags($tempStr); //得到去html标签的字符窜
    $strArray = explode('||', $tempStr); //将字符窜按分割符进行分割foreach($strArray as $v)
    {
    $tempStr = str_replace('a', '<b>a</b>', $v); //替换a为<b>a</a>
    $str = str_replace($v, $tempStr, $str); //替换原字符每个html标签内容
    }echo $str;
      

  2.   

    $str = <<<STR
    <div class="abc">
        <a href="#a">go to a</a>
        <img src="images/a.gif" alt="this is image a" />
        <p>
            <span>caption</span>
            <span class="content">Application</span>
        </p>
    </div>
    STR;$reg = '/<.+?>.+<.+?>/i';preg_match_all($reg, $str, $array);$array = array_unique($array[0]);$len = count($array);for($i = 0;$i < $len;$i++)
    {
        preg_match('/<.+?>(.+)<.+?>/i', $array[$i], $a);
        $inner = preg_replace('/(a)/i','<b>\1</b>', $a[1]);
        $outer = preg_replace('/(<.+?>).+(<.+?>)/i', '\1' . $inner . '\2', $a[0]);
        $str = str_replace($a[0], $outer, $str);
    }echo $str;
      

  3.   

    上面那个有问题。这个用正则不太好实现哦。我自己写了个。$str = <<<STR
    <div class="abc">
        <a href="#a">go to a<div>go to span</div></a>
        <a><div>这是一a个测试</div></a>
        <img src="images/a.gif" alt="this is image a" />
        <p>
            <span>caption</span>
            <span class="content">Application</span>
        </p>
    </div>
    STR;function getTag(&$html, &$pos, $len)
    {
        $sp = $pos;
        $rs = '<';
        $pos++;
        $c = $html[$pos];
        while(true)
        {
            if($c === '<')
            {
                $pos = $sp;
                return false;
            }
            $rs .= $c;
            $pos++;
            if($pos >= $len)
            {
                return false;
            }
            $c = $html[$pos];
            if($c === '>')
            {
                break;
            }
        }    $rs .= '>';    return $rs;
    }function replace($html)
    {
        $len = strlen($html);    $rs = '';
        $pos = 0;
        $c = $html[$pos];
        while(true)
        {
            if($c === '<')
            {
                $gr = getTag($html, $pos, $len);
                if($gr !== false)
                {
                    $rs .= $gr;
                }
            }
            else if($c === 'a' || $c === 'A')
            {
                $rs .= '<b>' . $c . '</b>';
            }
            else
            {
                $rs .= $c;
            }        $pos++;
            if($pos >= $len)
            {
                break;
            }
            $c = $html[$pos];
        }    return $rs;
    }$str = replace($str);echo $str;
      

  4.   

    PHP版
    <?php
    $str = 'A<div class="abc">
        <a href="#a">go to a</a>
        <img src="images/a.gif" alt="this is image a" />
        <p>
            <span>caption</span>
            <span class="content">application</span>
        </p>
    </div>aa
    ';
    $str = preg_replace_callback('/(?<=^|>).+(?=<|$)/U',create_function('$ms','return preg_replace("/a/i","<b>$0</b>",$ms[0]);'),$str);echo $str;
    ?>
      

  5.   

    JS版
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title> new document </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="generator" content="editplus" />
    <meta name="author" content="JnKc" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <style type="text/css">
    *{font-size:22px;}
    </style>
    </head>
    <body>
    <div class="abc">
        <a href="#a">go to a</a>
        <img src="images/a.gif" alt="this is image a" />
        <p>
            <span>caption</span>
            <span class="content">application</span>
        </p>
    </div>
    </body>
    </html>
    <script type="text/javascript">
    document.body.innerHTML = document.body.innerHTML.replace
    (/(>)([^<]+)(<)/gim,
      function f2c($0,$1,$2,$3){
    return($1+$2.replace(/a/gim,'<b>a</b>')+$3);
      }
    );
    </script>