Hi Guys,

what i want in php may be a simple thing for all you expert people :-), but i am stuck in this problem.

what I want is , to search for a word in the strings which starts from character "@".

For example string is : "hey people, @google is a great search engine," so in this string, i want to search for this "@google" word and then i will put some code to make it a link so that it redirects us to Google.com or some other links.

If anyone has some other way to make this word a link like @tweetid is automatically linked to user id on twitter, then please post it as reply.

Thanks.

Well, It worked superb, It was great to get a perfect answer in this much short time, I am extremely thankful to you. :-)

Hello! That is easy to do with a regular expression (regex)!

$string = $tweet;
$string = preg_replace('/([@])([^ .?!"]+)/', '<a href="http://twitter.com/\\2">\\2</a>', $string);
echo "$replace";

I didn't test the code, so there could be a mistake in it! Let me know if it worked.

This also may be helpful: http://www.macronimous.com/resources/writing_regular_expression_with_php.asp.

You're very welcome, but there is a problem I just discovered, sorry about that.
The code I provided only replaces @-mentions followed by .?!" or a space.
But what, if you're tweets or your re-tweets are followed by ,/;...?

The better solution would probably be:

$string = preg_replace('/([@])([A-Za-z0-9]+)/', '<a href="http://twitter.com/\\2">\\2</a>', $string);

You may also want to think of replacing #-searches:

$string = preg_replace('/([#])([A-Za-z0-9]+)/', '<a href="http://twitter.com/#!/search?q=%23\\2">\\2</a>', $string);

WOW! Well. that was something about which only experts like you can think of :-),
Thanks a lot for the additional information. Do you have any idea about how links can be posted in tweets? i am using text area to post the tweets but links are not working in the tweets. they just appear as plain text, would be very helpful if you know it.

Thank again.

-----------------------------------------------------------------------------

You're very welcome, but there is a problem I just discovered, sorry about that.
The code I provided only replaces @-mentions followed by .?!" or a space.
But what, if you're tweets or your re-tweets are followed by ,/;...?

The better solution would probably be:

$string = preg_replace('/([@])([A-Za-z0-9]+)/', '<a href="http://twitter.com/\\2">\\2</a>', $string);

You may also want to think of replacing #-searches:

$string = preg_replace('/([#])([A-Za-z0-9]+)/', '<a href="http://twitter.com/#!/search?q=%23\\2">\\2</a>', $string);

That depends on what type of links you want to replace.
For twitter I would suggest only www-links and http-links (and not email addresses), but this is a little more complex.

$string='http://site.com https://site.net http://site.me/index.php www.site.de/home www.site.co.uk/Images/logo.png';

$string = preg_replace('/((([a-zA-Z]{3,5}):\/\/)(([-.?!]*)?[^ \"\n\r\t<.,:!?)])*)/', '<a href="\\1">\\1</a>', $string);
$string = preg_replace('/(^|[\n (>])(([wW]{3})(([-.?!]*)?[^ "\n\r\t<.,:?!)])*)/', '\\1<a href="http://\\2">\\2</a>', $string);

echo "$string";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.