用preg replace移除锚标记后的空格


Removing spaces after anchor tag with preg replace

我想在锚标记后面放一个空格,以便下一个单词与它分开。问题是有锚标记之后,有 字符或可能有另一个html标签打开。因此,在这些情况下,我们不想放入space,因为它会打破我们的记录。

我只想在没有空格且有单词的情况下在锚后加上空格。

现在我已经提出了正则表达式,我不确定这是我到底想要什么

 preg_replace("/'<'/a'>([^'s<&nbsp;])/", '</a> $1', $text, -1, $count);
 print "Number of occurence in type $type = $count 'n";
 $this->count += $count;

在实际保存替换的字符串之前,我试图查看出现的次数。但它显示了更高的金额,我高度怀疑这是不可能的。

请帮我修复这个正则表达式。

场景:

<a href="blah.com">Hello</a>World // Here we need to put space between Hello and World
<a href="blah.com">Hello</a>&nbsp;World // Do not touch this
<a href="blah.com">Hello</a><b>World</b> // do not touch this

可能有很多情况需要忽略,但具体来说,我们需要执行第一种情况

@trincot指出,如果[^'s<&nbsp;]不是空格或非分隔符,则不表示。它是一个字符类,括号之间的值只有一个字符的平均值。如果不是space < &或者。

您需要检查下一个字符是否为表示[a-zA-Z0-9_]的单词字符'w,然后考虑在使用的正向前看的零宽度断言处添加一个空格:

 preg_replace("~</a>'K(?='w)~", ' ', $text, -1, $count);
 echo "Number of occurrences in type $type is $count 'n";

这个正则表达式是什么意思?

</a>    # Match closing anchor tag
'K      # Reset match
(?='w)  # Look if next character is a word character

更新:另一个覆盖所有html问题的解决方案:

preg_replace("~</a>'K(?!&nbsp;)~", '&nbsp;', $text, -1, $count);

当结束锚标记后没有非换行空格时,添加一个非换行空格。

您可能会发现,regex解决方案迟早会被证明是不够的。例如,它不会检测到在这个HTML片段中显示的两个单词之间没有空格:

<a>test</a><span>hello</span>

在许多其他情况下,regex解决方案很难检测到类似的相邻单词,因为HTML的呈现并不像看起来那么简单。

虽然您已经接受了一个解决方案,但我在这里提供一个解决方案,它使用PHP中可用的DOMDocument接口来检测链接文本粘附在后面的文本的位置,即使它在DOM节点层次结构中与它远程分离:

function separateAnchors($html) {
    // Define a character sequence that 
    // will certainly not occur in your document,
    // and is interpreted as literal in regular expressions:
    $magicChar = "²³²"; 
    $doc = new DOMDocument();
    $doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    $xpath = new DOMXPath($doc);
    $anchors = $xpath->query("//a");
    foreach (array_reverse(iterator_to_array($anchors)) as $anchor) {
        $parent = $anchor->parentNode;
        $origAnchor = $anchor->cloneNode(true);
        // temporariy put the special text in the anchor
        $anchor->textContent = $magicChar;
        // and then take the document's text content
        $txt = $doc->textContent;
        // If that contains the special text with a non-space following it:
        if (preg_match("/{$magicChar}'S/u", $txt)) {
            // ... then add a single space node after it, after
            // any closing parent nodes
            $elem = $anchor;
            while (!$elem->nextSibling) $elem = $elem->parentNode;
            $elem->parentNode->insertBefore($doc->createTextNode(" "), 
                                            $elem->nextSibling);
        }
        // Put original anchor back in place
        $parent->replaceChild($origAnchor, $anchor);
    }
    return $doc->saveHTML();
}
// sample data
$html = "<p><a>first link</a>&nbsp;<a>second link</a>this word is too close</p>'n
         <table><tr><td><a>table cell</a></td></tr></table><span>end</span>'n
         <span><a>link</a></span><span><a>too close</a></span>";
// inject spaces
$html = separateAnchors($html);
// Show result
echo $html;

看它运行在ideone.com

您可以使用:/(?<=<'/a>)('w+)/g regex

含义:找到结束锚标记前面的单词,并用空格和第一个捕获组引用($1)替换它

使用的每个结构的示例和含义