Jump to content

[SOLVED] Find URLS that are not within [url] tags


ionik

Recommended Posts

Ok what i have is a regex that will find all links in a text string and convert them into clickable links I.E http://website.com into <a href="http://website.com">http://website.com</a> what i need is so that will not turn links that are included in tags into clickable links

im using a regex found here

~(?<!url)(??:(??:https?|ftp)://)|www\.)|\S+\@)(?:\S+\.\S+)(?<!\p{P})~

What i need is for if a url is like this

[ url]http://website.com[/url]
//or 
[ url=http://website.com]http://website.com[/url]

to be excluded from the regex

 

Try this:

 

<pre>
<?php
$data = <<<DATA
http://www.google.com
[url]http://website.com[/url]
[url=http://website.com]http://website.com[/url]
http://www.phpfreaks.com!
DATA;

echo preg_replace('%
	### Not preceded by an url start.
	(?<!url[=\]])
	(
		### Protocol or start.
		(?:
			(??:https?|ftp)://)
			|
			www\.
		)
		### Body.
		(?>
			### Gobble all non-space with the exception of [/url]
			(??!\[/url\])\S)+
			### Avoid ending punctuation.
			(?<!\p{P})
		)
		### Not followed by an url end.
		(?!\[/url\])
	)
%x', '<a href="$1">$1</a>', $data);

?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.