flex flex - 14 days ago 5
PHP Question

change string where find @username in php

How to replace @username in a complete string where find.

Example:
Original string :

"Hello @mike and @max how are you";


Expected result string :
"Hello <a href="#">@mike</a> and <a href="#">@max</a> how are you'


I am tried to use strpos and explode, but not got solution.

Please help any one! Thanks.

Answer

Try this regex:

$Output = preg_replace("/(\@\w+)/", "<a href='#'>$1</a>", $Original_String);

http://www.phpliveregex.com/p/hVf

Comments