View Full Version : parse links in mysql text area
chadvirus
7-27-04, 12:37 PM
this seems like a pretty basic problem, which should have a simple answer -- be it some sort of free application or code. i'd like to have my text areas in mysql when they are displayed on a php page automatically converted so any word that starts with http://www is automatically converted to a real link by parsing <a href=""> tags around it when the database results are returned. like they are in this very forum.
also, is there a way to detect line breaks in text areas in mysql tables and make them <p> or hard returns so the lines don't run into each other? (ex--
paragraph 1
paragraph 2
does not show up as paragraph 1paragraph 2
when results are returned)
thanks in advance.
nl2br() will take care of the line breaks. Check out the user contributed notes for preg_replace in the manual regarding the links.
chadvirus
7-27-04, 01:13 PM
i just came across that. thanks!
for parsing the link, just search for links within submitted text using a regex and use str_replace() to replace the string with <a href="string">string</a>
str_replace() -- http://us4.php.net/manual/en/function.str-replace.php
maintaining line spacing = nl2br() -- http://us4.php.net/manual/en/function.nl2br.php
EDIT: oops, a little late
$data being the variable storing the url's to parse
//$data = preg_replace("/(http:\/\/){1}(www\.)?([a-z0-9A-Z]){2,}\.([a-zA-Z]){2,3}\/?/", "<a href='\\0'>\\0</a>", $data); // < PHP 4.0.4
$data = preg_replace("/(http:\/\/){1}(www\.)?([a-z0-9A-Z]){2,}\.([a-zA-Z]){2,3}\/?/", "<a href='$0'>$0</a>", $data); // >= PHP 4.0.4
$data = preg_replace("/(http:\/\/)((www\.)|([a-z0-9A-Z]\.)+)?(([a-z0-9A-Z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]+)?)){2,}\.([a-zA-Z]){2,3}\/?/", "<a href='\\0'>\\0</a>", $data);
could try this line instead... it supports multiple subdomains and an ip address w/port as an alternative to a hostname
$data = preg_replace("/(http:\/\/)((www\.)|([a-z0-9A-Z]\.)+)?(([a-z0-9A-Z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(:[0-9]+)?)){2,}\.([a-zA-Z]){2,3}\/?/", "<a href='$0'>$0</a>", $data);
again... for versions over 4.0.4
vBulletin v3.6.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.