【问题标题】:preg_replace href if url inside href does not contain particular domain [duplicate]如果 href 中的 url 不包含特定域 [重复],则 preg_replace href
【发布时间】:2018-03-12 05:24:54
【问题描述】:

我需要从文本中删除 <a href,除非 href 中的域/url 包含 example.com

我找到了这个正则表达式,但它现在替换了所有 urls (.*?) ,任何想法如何使它替换所有hrefs except如果url包含example.com

preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $_POST['desc']);

这是替换应该如何工作的示例

<a href="http://example.com/test">test1</a> ==> Should not be replaced (targeted) <a href="http://google.com/test">test2</a> ==> test2

【问题讨论】:

    标签: php url preg-replace href


    【解决方案1】:
    ['"]https?:\/\/(?!example\.com)[^"']+['"]
    

    该正则表达式将匹配除域为 example.com 的所有 URL 在这种情况下,我假设它可能被封装在单引号或双引号之间,例如 href="{url}" 或 href='{url}'。

    【讨论】:

    猜你喜欢
    • 2017-09-20
    • 1970-01-01
    • 2013-03-30
    • 2021-03-21
    • 2014-03-08
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    相关资源
    最近更新 更多