Hello... I'm new here (and fairly new to php) and hungover so be patient.
Page in question: http://www.dementedmoth.co.uk/
Problem: I'm using wordwrap - wordwrap($text, 60, "\n", 1); - to wrap my text with live URL's.
That's fine http://www.dementedmoth.co.uk/found_cont.php
But when it comes to adding a nl2br it kills the longer links. If I use a " " instead of "\n" in the wordwrap it kind of works, but corrupts a few of the longer links - see the link to this forum in particular:
http://www.dementedmoth.co.uk/found_cont_nl2br.php
This is the function for detecting the links:
function get_urls($row_get_found)
{ $url_tags = array (
"'<a[^>]*>.*</a>'si",
"'<map[^>]*>.*</map>'si",
"'<script[^>]*>.*</script>'si",
"'<style[^>]*>.*</style>'si",
"'<applet[^>]*>'si",
"'<object[^>]*>'si",
"'<embed[^>]*>'si",
"'<link[^>]*>'si",
"'<img[^>]*>'si"
);
foreach($url_tags as $url_tag)
{
preg_match_all($url_tag, $row_get_found, $matches, PREG_SET_ORDER);
foreach($matches as $match)
{
$key = "<" . md5($match[0]) . ">";
$search[] = $key;
$replace[] = $match[0];
}
}
$row_get_found = str_replace($replace, $search, $row_get_found);
// indicate where urls end if they have these trailing special chars
$sentinals = array("'&(quot|#34);'i", // Replace html entities
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(d+);'e");
$row_get_found = preg_replace($sentinals, "<sentinal>\0<sentinal>", $row_get_found);
$vdom = "[:alnum:]"; // Valid domain chars
$vurl = $vdom."_~-"; // Valid subdomain and path chars
$vura = "Γ-ΓΓ -ΓΏ!#$%&*+,;=@./".$vurl; // Valid additional parameters (after '?') chars;
// insert other local characters if needed
$protocol = "[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]"; // Protocol exp
$server = "([$vurl]+[.])*[$vdom]+"; // Server name exp
$path = "(([$vurl]+([.][$vurl]+)*/)|([.]{1,2}/))*"; // Document path exp (/.../)
$name = "[$vurl]+([.][$vurl]+)*"; // Document name exp
$params = "[?][$vura]*"; // Additional parameters (for GET)
$row_get_found = eregi_replace("$protocol$server(/$path($name)?)?($params)?", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $row_get_found); // URL into links
$row_get_found = eregi_replace('([[:space:]?\n?>()[{}])([url]www.[/url][-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2" target="_blank">\\2</a>', $row_get_found);
$row_get_found = str_replace("<sentinal>", '', $row_get_found);
return str_replace($search, $replace, $row_get_found);
}
Calling it with:
$text = get_urls ($row_get_found['post']);
$newtext = wordwrap($text, 60, " ", 1);
echo nl2br ($newtext);
If anyone can give me any pointers, that would be grand. Another option would be to detect the live urls and just have them shortened as such: "http://www.domai..."