...

View Full Version : Resolved Regular Expressions - URL's that DON'T have a tag?



AIOBB-Richard
01-17-2010, 11:07 PM
Hello,

Here is an example of the data I have:


Test http://yay2.com (http://yay.com) http://yay3.com http://www.yay4.com

This is what I want the output to be:


Test http://yay2.com (http://yay.com) <a href="http://yay3.com">http://yay3.com</a> <a href="http://www.yay4.com">http://www.yay4.com</a>

As you can see, I want the code to ignore the url's inside the [url] tags... I have this:


function parse_urls($string)
{
$host = "([a-z\d][-a-z\d]*[a-z\d]\.)+[a-z][-a-z\d]*[a-z]";
$port = "(:\d{1,})?";
$path = "(\/[^?<>\#\"\s]+)?";
$query = "(\?[^<>\#\"\s]+)?";
$string = preg_replace("#(?!url=)((ht|f)tps?:\/\/{$host}{$port}{$path}{$query})(?!\[\/url\])#i", "<a href=\"$1\">$1</a>", $string);
return $string;
}

But it does not seem to work correctly, it updates all the links, including the ones in the [url] tags. What am I doing wrong?

Thanks.

JAY6390
01-18-2010, 03:12 AM
$string = preg_replace('%\[url\](.*?)\[/url\]%', '<a href="$1">$1</a>', $string);
That should do it

AIOBB-Richard
01-18-2010, 04:43 AM
$string = preg_replace('%\[url\](.*?)\[/url\]%', '<a href="$1">$1</a>', $string);
That should do it

Nope, that does not work at all.

AIOBB-Richard
01-18-2010, 05:08 AM
Hey,

Just noticed that this forum parsed my link's and added url tags where there was not meant to be any.

What I need, is a regex to pick up URL's NOT already in url tags. so
http://google.com/ is NOT picked up, but http://google.com is when used not in a url tag.

AIOBB-Richard
01-18-2010, 05:56 AM
Thanks for the help guys, decided to do it the long way;


function parse_urls($string, $rel = 'nofollow')
{
$host = "([a-z\d][-a-z\d]*[a-z\d]\.)+[a-z][-a-z\d]*[a-z]";
$port = "(:\d{1,})?";
$path = "(\/[^?<>\#\"\s]+)?";
$query = "(\?[^<>\#\"\s]+)?";
preg_match_all("#((ht|f)tps?:\/\/{$host}{$port}{$path}{$query})#i", $string, $patts, PREG_OFFSET_CAPTURE);

$patts[0] = array_reverse($patts[0]);

foreach ($patts[0] as $value)
{
if (strtoupper(substr($string, $value[1] - 5, 5)) != " + strlen($value[0]), 6)) != " (")
{
if (strtoupper(substr($string, $value[1)" )
{
// Replace away...
$string = substr_replace($string, "<a href=\"".$value[0]."\" target=\"_blank\" rel=\"{$rel}\">".$value[0]."</a>", $value[1], strlen($value[0]));
}
}
}

return $string;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum