how to convert this ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

how to convert this ??

Post by PHPycho »

Hello forums !!
suppose i had following string

Code: Select all

$string = "hello friends . My site is http://$sitenamefromdb.com";
I want to know:
1> how to convert the string: http://example.com is converted into a link .
ie when $string is displayed ie

Code: Select all

echo $string;
, a link should appear instead of plain text. ie <a href="http://$sitenamefromdb.com">http://$sitenamefromdb.com</a>
Note: if the $sitenamefromdb was static i could solve using str_replace, since its dynamic as it comes from the db. i didnt get the idea..
i think we should search http:// or www. in a word of a string and replace that word...
Please help me..
Thanks in advance to all of you !!
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

what you have in DB?
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

just the name of site ..for ex. hotmail
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

Use sprintf()
OR try something like

Code: Select all

$sitenamefromdb = "hotmail";
$string = "hello friends . My site is <a href='http://{$sitenamefromdb}.com'>http://{$sitenamefromdb}.com</a>";
echo $string;
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So their is not protocal associated with the domain? (meaning you have hotmail, amazon, yahoo, etc without the http(s):// and .tld part?)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Use preg_replace to search for a URL match and replace it with a link. Something like this (untested)

Code: Select all

$linked = preg_replace('~http://\S+~', '<a href="$0">$0</a>', $input);
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

Thanks mr ole
Post Reply