简体   繁体   English

用 href 链接替换匹配的文本

[英]Replace matching text with href links

I have a string like:我有一个像这样的字符串:

@test hello how are you @abcdef

How would I automatically make it so it converts all text that has @ to something like:我将如何自动制作它以便将所有具有 @ 的文本转换为类似以下内容:

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

I've tried using regex and preg_replace but can't get it down perfectly.我试过使用 regex 和 preg_replace 但无法完美地完成它。

Try regex @(\S+) with substitution https://example.com/$1 :尝试用正则表达式@(\S+)替换https://example.com/$1

$string = '@test hello how are you @abcdef';

$result = preg_replace('/@(\S+)/', 'https://example.com/$1', $string);

print($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM