1

好的,这是我的情况……我在我的 vBulletin 论坛上安装了一个词汇表插件。如果在论坛上找到一个术语,它将用指向词汇表定义的链接替换该术语。

这是附加组件使用的正则表达式代码:

$findotherterms[] = "#\b$glossaryname\b(?=\s|[.,?!;:]\s)#i";
$replacelinkterms[] = "<span class=\"glossarycrosslinkimage\"><a href=\"$glossarypath/glossary.php?do=viewglossary&amp;term=$glossaryid\"' onmouseover=\"glossary_ajax_showTooltip('$glossarypath/glossary_crosslinking.php?do=crosslink&term=$glossaryid',this,true);return false\" onmouseout=\"glossary_ajax_hideTooltip()\"><b>$glossaryname&nbsp;</b></a></span>";
$replacelinkterms[] = "<a href=\"glossary.php?q=$glossaryname\">$glossaryname</a>";
$glossaryterm = preg_replace($findotherterms, $replacelinkterms, $glossaryterm, $vbulletin->options['vbglossary_crosslinking_limit']);
return $glossaryterm;

问题是,如果论坛帖子中有一个包含现有术语的链接,插件将在链接内创建一个链接......

所以让我们说“测试”是一个词汇表,我有这个论坛帖子:

some forum post including <a href="http://www.test.com">test</a> link

该插件会将其转换为:

some forum post including <a href="http://www.<a href="glossary.php?q=test">test</a>.com"><a href="glossary.php?q=test">test</a> link

那么,如果在现有链接中找到字符串,我如何修改此代码以不替换任何内容?

4

1 回答 1

3

描述

最好用您想要替换的好字符串实际捕获您不想替换的坏字符串,然后简单地应用一些逻辑。

在这种情况下,正则表达式将:

  • <a ...>找到从 open到 close的所有锚标签</a>。因为这是正则表达式中的第一个,它将捕获test存在于锚标记内的所有不需要的字符串。
  • 查找所有字符串test,注意这部分可以替换|为所有词汇表术语的分隔列表。该值被插入到捕获组 1。

/<a\b(?=\s)(?:[^>=]|=\'[^\']*\'|="[^"]*"|=[^\'"\s]*)*"\s?>.*?<\/a>|(test)

在此处输入图像描述

然后 PHP 逻辑根据是否找到捕获组 1 选择性地替换文本。

PHP 示例

现场示例:http: //ideone.com/jpcqSR

代码

    $string = 'some forum test post including <a href="http://www.test.com">test</a> link';
    $regex = '/<a\b(?=\s) # capture the open tag
(?:[^>=]|=\'[^\']*\'|="[^"]*"|=[^\'"\s]*)*"\s?> # get the entire tag
.*?<\/a>
|
(test)/imsx';

    $output = preg_replace_callback(
        $regex,
        function ($matches) {
            if (array_key_exists (1, $matches)) {
                return '<a href="glossary.php?q=' . $matches[1] . '">' . $matches[1] . '<\/a>';
            }
            return $matches[0];
        },
        $string
    );
    echo $output;

更换前

some forum test post including <a href="http://www.test.com">test</a> link

更换后

some forum <a href="glossary.php?q=test">test<\/a> post including <a href="http://www.test.com">test</a> link

于 2013-07-06T05:14:02.100 回答