Ask about a PHP problem here.
irfanh94
Posts: 26 Joined: Thu May 05, 2011 7:43 pm
Post
by irfanh94 » Fri May 06, 2011 12:05 am
Ok, im gonna make first topic and post in php help..
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..
jacek
Site Admin
Posts: 3262 Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:
Post
by jacek » Fri May 06, 2011 12:10 am
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 ?
irfanh94
Posts: 26 Joined: Thu May 05, 2011 7:43 pm
Post
by irfanh94 » Fri May 06, 2011 12:45 am
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
Post
by Tino » Fri May 06, 2011 3:37 am
No. You'll have to mark it as a link, otherwise it won't behave as a link.
jacek
Site Admin
Posts: 3262 Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:
Post
by jacek » Fri May 06, 2011 9:54 am
oh you mean like auto linking when someone types a url in a message or something ?
irfanh94
Posts: 26 Joined: Thu May 05, 2011 7:43 pm
Post
by irfanh94 » Fri May 06, 2011 10:55 am
Yes, like on forums..
jacek
Site Admin
Posts: 3262 Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:
Post
by jacek » Fri May 06, 2011 11:22 am
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.