简体   繁体   中英

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.

Try regex @(\S+) with substitution https://example.com/$1 :

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

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

print($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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