PHP url from table/database

Ask about a PHP problem here.
Post Reply
irfanh94
Posts: 26
Joined: Thu May 05, 2011 7:43 pm

PHP url from table/database

Post by irfanh94 »

Ok, im gonna make first topic and post in php help.. :D

How can i make every link that came out from database to display as link..

Exm: i post to the database new text and add the link http://www.adsad.com.. And after that i want to display link as real link, not like text..
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP url from table/database

Post by jacek »

You need to output it as a link tag, ie
$result = mysql_query('SELECT `link` FROM `table`');

while ($row = mysql_fetch_assoc($result)){
    echo "<a href=\"{$row['link']}\">{$row['link']}</a>";
}
If that's what you meant ?
Image
irfanh94
Posts: 26
Joined: Thu May 05, 2011 7:43 pm

Re: PHP url from table/database

Post by irfanh94 »

I know for that, but how to make output as a link if i dont type the code for link in file..? Is that possible?
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: PHP url from table/database

Post by Tino »

No. You'll have to mark it as a link, otherwise it won't behave as a link.
Please check out my CodeCanyon items.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP url from table/database

Post by jacek »

oh you mean like auto linking when someone types a url in a message or something ?
Image
irfanh94
Posts: 26
Joined: Thu May 05, 2011 7:43 pm

Re: PHP url from table/database

Post by irfanh94 »

Yes, like on forums..
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP url from table/database

Post by jacek »

Ah, that's a bit more tricky.

You need to use a regular expression to match things that look like URLs and link them.

Like this...
<?php

$text = <<<TEXT
Hi

look a url http://google.com/

it should be linked.
TEXT;

echo preg_replace("#((?:https?|ftp|gopher|telnet|file|notes|ms-help):(?://|\\\\)+[\w\d:\#@%/;$\(\)~_?\+-=\\\.&]*)#", '<a href="$1">$1</a>', $text);

?>
regex from here: http://www.geekzilla.co.uk/view2D3B0109 ... BC85FD.htm and slightly modified to work with php's preg_ functions.
Image
Post Reply