$str = preg_replace("/(?<!\"[^>]*)(茶叶)(?!\"|<\/a>)/sui", '<a href="555.html">茶叶</a>', $str, 5); 报错如下:
Warning: preg_replace() [function.preg-replace]: Compilation failed: lookbehind assertion is not fixed length at offset 10 in D:\AppServ\www\Template\test.php on line 28

解决方案 »

  1.   

    $str = preg_replace("/(?!\"[^>]*)(茶叶)(?!\"|<\/a>)/i", '<a href="555.html">茶叶</a>', $str, 5);  
      

  2.   

    那你要什么效果? php不支持?< 这种格式。最好贴出你的实例。
      

  3.   

    给关键词加超链接,但忽略已有的超链接,及所有标签属性的内容,举个例子:条件:给所有带茶叶的关键词加链接
    原字符串内容:这儿是茶叶的链接。
    <img src="中国好茶叶.jpg" width="120" height="120" alt="中国好茶叶" />
    <span title="中国好茶叶">中国茶叶</span>
    这儿是中国茶叶大观的链接。
    这儿是<a href="原有的链接.html">茶叶</a>的现有链接。要实现的效果:这儿是<a href="新加的链接.html">茶叶</a>的链接。
    <img src="中国好茶叶.jpg" width="120" height="120" alt="中国好茶叶" />
    <span title="中国好茶叶">中国<a href="新加的链接.html">茶叶</a></span>
    这儿是中国<a href="新加的链接.html">茶叶</a>大观的链接。
    这儿是<a href="原有的链接.html">茶叶</a>的现有链接。
      

  4.   

    简单的处理方法就是用preg_replace_callback把双引里面的数据替换并保存到一变量$save。
    接着直接str_replace('茶叶', '<a href="新加的链接.html">茶叶</a>', $str)。
    最后恢复$save的数据。当然能一次正则能搞定最好拉。。不过应该不好写。正则达人上吧
      

  5.   

    试试
    $html = <<<html
    这儿是茶叶的链接。
    <img src="中国好茶叶.jpg" width="120" height="120" alt="中国好茶叶" />
    <span title="中国好茶叶">中国茶叶</span>
    这儿是中国茶叶大观的链接。
    这儿是<a href="原有的链接.html">茶叶</a>的现有链接
    html;
    echo preg_replace('#(?=[^>]*(?=<(?!/a>)|$))茶叶#','<a href="新加的链接.html">\0</a>',$html);
    }
      

  6.   

    谈不上厉害,就是平时喜欢摆弄摆弄,呵呵。
    还有php不是不支持?<这样的零宽,而是后边必须跟着固定宽度的正则串,所以爆了is not fixed length的错误,意思是?<!后的正则串不能有*,+,{1,}等等不固定宽度的正则串。