Page 1 of 1

PHP url from table/database

Posted: Fri May 06, 2011 12:05 am
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..

Re: PHP url from table/database

Posted: Fri May 06, 2011 12:10 am
by jacek
You need to output it as a link tag, ie

[syntax=php]$result = mysql_query('SELECT `link` FROM `table`');

while ($row = mysql_fetch_assoc($result)){
echo "<a href=\"{$row['link']}\">{$row['link']}</a>";
}[/syntax]

If that's what you meant ?

Re: PHP url from table/database

Posted: Fri May 06, 2011 12:45 am
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?

Re: PHP url from table/database

Posted: Fri May 06, 2011 3:37 am
by Tino
No. You'll have to mark it as a link, otherwise it won't behave as a link.

Re: PHP url from table/database

Posted: Fri May 06, 2011 9:54 am
by jacek
oh you mean like auto linking when someone types a url in a message or something ?

Re: PHP url from table/database

Posted: Fri May 06, 2011 10:55 am
by irfanh94
Yes, like on forums..

Re: PHP url from table/database

Posted: Fri May 06, 2011 11:22 am
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...

[syntax=php]<?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);

?>[/syntax]

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