In this post I will show you how to search a word start with ‘@’ symbol and replace with some other text or url.
For example the string is, “hai @kanbarasan how are you?
”, so in this string I want to search for this ‘@kanbarasan’
word and then I will put some code to make it a link so that it redirects us to http://twitter.com/kanbarasan
or some other links.
I used php preg_replace
function to find this and replace with url.Example:
echo $text = ‘Hai @kanbarasan how are you?’;
echo $tweet = preg_replace('/([@])([^ .?!":]+)/', '<a href="http://twitter.com/$2" >@$2</a>', $text);
Hai @kanbarasan how are you
how are you