解决方案 »

  1.   


    header("Content-type: text/html; charset=utf-8");
    $str = <<< STR
    <span><img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" />
        发型师
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" /></span>
        发型师
    STR;
    $str = strip_tags($str);
    $pattern = "/(?:发型师)/";
    preg_match_all($pattern, $str, $match);
    print_r($match);
    print_r(array_count_values($match[0]));
    print_r(array_unique($match[0]));
      

  2.   


    $str='<span><img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="听相声,学连带销售" alt="听相声,学连带销售" />发型师<img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="听相声,发型师,学连带销售" alt="听相声,学连带销售" /></span>';
    preg_match_all('/title="[^"]*?(发型师)+[^"]*?"/s',$str,$m);
    /*
    匹配非正文关键词
    Array
    (
        [0] => Array
            (
                [0] => title="听相声,发型师,学连带销售"
            )    [1] => Array
            (
                [0] => 发型师
            ))
    */$str='<span><img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="听相声,学连带销售" alt="听相声,学连带销售" />发型师<img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="听相声,学连带销售" alt="听相声,学连带销售" /></span>';
    preg_match_all('/>[^<]*?(发型师)+[^<]*?</s',$str,$m);
    /*
    匹配正文关键词
    Array
    (
        [0] => Array
            (
                [0] => >发型师<
            )    [1] => Array
            (
                [0] => 发型师
            ))
    */
      

  3.   

    另外,我想要只替换掉”非<a>链接标签对“中的关键词,有办法吗?还有,如果要只替换掉除了下面2种情况的所有关键词,要怎么写正则?
    排除1:<a>链接标签对中的关键词。比如<a href="xxxx">关键词</a>
    排除2:<img title="关键词" alt="关键词" />
      

  4.   

    其实我真正的目的不是只为匹配,而是要去替换,即只替换掉正文中关键词而不要替换掉标签中的关键词,这个有办法实现吗?
    header("Content-type: text/html; charset=utf-8");
    $str = <<< STR
    <span><img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" />
        发型师1
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" /></span>
        发型师2
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="按摩师,发型师" alt="发型师,学连带销售" /></span>
        发型师3
        </span>
    STR;
    //$str = strip_tags($str);
    //echo $str;
    $pattern = "/(.*?)(?:发型师)(.*?)/";
    preg_match_all($pattern, $str, $match);
    $keyword = '厨师';
    if (isset($match[0])) {
        foreach($match[0] as $v) {
            if (trim($v) == '发型师')
                $str = str_replace ($v, $keyword, $str);
        }
    }
    echo $str;
      

  5.   

    其实我真正的目的不是只为匹配,而是要去替换,即只替换掉正文中关键词而不要替换掉标签中的关键词,这个有办法实现吗?
    header("Content-type: text/html; charset=utf-8");
    $str = <<< STR
    <span><img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" />
        发型师1
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" /></span>
        发型师2
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="按摩师,发型师" alt="发型师,学连带销售" /></span>
        发型师3
        </span>
    STR;
    //$str = strip_tags($str);
    //echo $str;
    $pattern = "/(.*?)(?:发型师)(.*?)/";
    preg_match_all($pattern, $str, $match);
    $keyword = '厨师';
    if (isset($match[0])) {
        foreach($match[0] as $v) {
            if (trim($v) == '发型师')
                $str = str_replace ($v, $keyword, $str);
        }
    }
    echo $str;

    这个算法是不严谨的,只不过恰好匹配出来的“发型师1/23”前面是空格,所以才让这段代码看起来很正常。你匹配下面这段HTML代码试下就知道了。
    $str = <<< STR
    <span>
    <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" />发型师1
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" />发型师2
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="按摩师,发型师" alt="发型师,学连带销售" />发型师3
    </span>
    STR;而且,不直接用正则表达式一次性搞定还得另外附加PHP代码去处理,这只能算是实在无法直接用正则搞定时的一个备选方案
      

  6.   

    $str = <<< STR
    <span>
    <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" />发型师1
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" />发型师2
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="按摩师,发型师" alt="发型师,学连带销售" />发型师3
    </span>
    STR;preg_match_all('/(?<=>).*(发型师[^<]*)/', $str, $match);
    print_r($match[1]);Array
    (
        [0] => 发型师1
        
        [1] => 发型师2
        
        [2] => 发型师3)
      

  7.   


    $str = <<< STR
    <span>
    <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" />
    发型师1,发型师11,技巧非常成熟的发型师22233333
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" />发型师2
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="按摩师,发型师" alt="发型师,学连带销售" />发型师3
    </span>
    <span><img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" />
        发型师44
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="发型师,发型师" alt="发型师,学连带销售" /></span>
        发型师55
        <img src="http://userdata.chinaceot.com/person_college/video/intro_embed/20141117232123_24862.jpg" width="500" height="334" title="按摩师,发型师" alt="发型师,学连带销售" /></span>
        发型师666
        </span>
    STR;
    $pattern = "/>\s*\w*(?:发型师)\s*\w*|\s*\w*(?:发型师)\s*\w*\s*</";
    $keyword = '厨师';
    $str = preg_replace_callback($pattern, function($match) use ($keyword){
        if(isset($match[0])) {
            $arr = explode("发型师", $match[0]);
            $s = implode($keyword, $arr);
            return $s;
        }
    }, $str);
    echo $str;