【问题标题】:PHP preg_replace: Replace all anchor tags in text with their href value with RegexPHP preg_replace:用正则表达式将文本中的所有锚标记替换为它们的 href 值
【发布时间】:2023-03-31 20:35:02
【问题描述】:

我想用它们的 href 值替换文本中的所有锚标记,但我的模式不能正常工作。

$str = 'This is a text with multiple anchor tags. This is the first one: <a href="https://www.link1.com/" title="Link 1">Link 1</a> and this one the second: <a href="https://www.link2.com/" title="Link 2">Link 2</a> after that a lot of other text. And here the 3rd one: <a href="https://www.link3.com/" title="Link 3">Link 3</a> Some other text.';
$test = preg_replace("/<a\s.+href=['|\"]([^\"\']*)['|\"].*>[^<]*<\/a>/i",'\1', $str);
echo $test;

最后的文字应该是这样的:

This is a text with multiple anchor tags. This is the first one: https://www.link1.com/ and this one the second: https://www.link2.com/ after that a lot of other text. And here the 3rd one: https://www.link3.com/ Some other text.

非常感谢!

【问题讨论】:

  • 真的想试试parse HTML with RegEx ...你确定吗?您是否首先在办公桌周围画了一个五角星以防万一?
  • @CD001 如果有更好、更简单的解决方案,我将不胜感激。
  • 更安全的是使用 DOM Parser; HTML 不是正则的,因此尝试使用正则表达式解析它通常会导致问题 - 这几乎就是我发布的链接所指出的......但有更多的风格、类和幽默;)

标签: php regex preg-replace


【解决方案1】:

别这样。

改用解析器。

$dom = new DOMDocument();
// since you have a fragment, wrap it in a <body>
$dom->loadHTML("<body>".$str."</body>");
$links = $dom->getElementsByTagName("a");
while($link = $links[0]) {
    $link->parentNode->insertBefore(new DOMText($link->getAttribute("href")),$link);
    $link->parentNode->removeChild($link);
}
$result = $dom->saveHTML($dom->getElementsByTagName("body")[0]);
// remove <body>..</body> wrapper
$output = substr($result, strlen("<body>"), -strlen("</body>"));

Demo on 3v4l

【解决方案2】:

如果您仍然使用正则表达式,这应该可以工作:

preg_replace("/<a\s+href=['\"]([^'\"]+)['\"][^\>]*>[^<]+<\/a>/i",'$1', $str);

但您可能最好使用 Andreas 发布的解决方案。

仅供参考:您之前的正则表达式不起作用的原因是这个小数字:

.*>

因为. 选择了您最终匹配到要替换的所有内容的所有内容;一直到最后。这就是为什么它似乎只选择并替换它找到的第一个锚标记并切断其余部分。

改成

[^\>]*

确保此特定选择仅限于存在于 url 和 a 标记的结束括号之间的字符串部分。

【讨论】:

    【解决方案3】:

    可能不是更简单,但更安全的是使用 strpos 循环字符串以查找并剪切字符串并删除 html。

    $str = 'This is a text with multiple anchor tags. This is the first one: <a class="funky-style" href="https://www.link1.com/" title="Link 1">Link 1</a> and this one the second: <a href="https://www.link2.com/" title="Link 2">Link 2</a> after that a lot of other text. And here the 3rd one: <a href="https://www.link3.com/" title="Link 3">Link 3</a> Some other text.';
    
    $pos = strpos($str, '<a');
    
    while($pos !== false){
        // Find start of html and remove up to link (<a href=")
        $str = substr($str, 0, $pos) . substr($str, strpos($str, 'href="', $pos)+6);
        // Find end of link and remove that.(" title="Link 1">Link 1</a>)
        $str = substr($str, 0, strpos($str,'"', $pos)) . substr($str, strpos($str, '</a>', $pos)+4);
        // Find next link if possible
        $pos = strpos($str, '<a');
    }
    echo $str;
    

    https://3v4l.org/vdN7E

    已编辑以处理 a-tag 的不同顺序。

    【讨论】:

    • 除非碰巧有像&lt;a class="funky-style" href="URI" ... /&gt;这样的东西...属性顺序没有限制所以...
    • @CD001 正在查看 OP 自己的正则表达式,似乎他想删除所有 href 链接并将链接保留为文本。如果不是这种情况,那么 OP 应该用一个更好的例子进行编辑。编辑:我现在明白你的意思了。是的,但又是一个更好的例子
    【解决方案4】:

    如果你想用 href 值替换标签,你可以这样做:

    $post = preg_replace("/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/","$1",$post);
    

    如果要替换为文本值:

    $post = preg_replace("/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/","$2",$post);
    

    【讨论】:

      猜你喜欢
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多