【问题标题】:Replace matching text with href links用 href 链接替换匹配的文本
【发布时间】:2023-01-12 10:14:41
【问题描述】:

我有一个像这样的字符串:

@test hello how are you @abcdef

我将如何自动制作它以便将所有具有 @ 的文本转换为类似以下内容:

https://example.com/test
https://example.com/abcdef

我试过使用 regex 和 preg_replace 但无法完美地完成它。

【问题讨论】:

    标签: php regex preg-replace


    【解决方案1】:

    尝试将正则表达式 @(S+) 替换为 https://example.com/$1

    $string = '@test hello how are you @abcdef';
    
    $result = preg_replace('/@(S+)/', 'https://example.com/$1', $string);
    
    print($result);
    

    【讨论】:

    • 如果有@abcdef<br>,你会如何建议去掉正则表达式中的 <br>?
    • 如果你想完全摆脱标签,试试@(w+)(?:&lt;[^&lt;&gt;]*&gt;)?之类的东西
    猜你喜欢
    • 2018-03-22
    • 1970-01-01
    • 2011-09-12
    • 2015-06-24
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 2011-11-25
    • 2011-05-01
    相关资源
    最近更新 更多