flex flex - 1 year ago 100
PHP Question

change string where find @username in php

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

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 Source

Try this regex:

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download