drzbc6003 2018-10-10 15:24
浏览 131
已采纳

PHP preg_replace:将文本中的所有锚标记替换为带有正则表达式的href值

I want to replace all anchor tags within a text with their href value, but my pattern does not work right.

$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;

At the end the text should look like this:

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.

Thank you very much!

  • 写回答

3条回答 默认 最新

  • dqw7121 2018-10-10 16:25
    关注

    Just don't.

    Use a parser instead.

    $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条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?