Ok, so for my forum, I'm trying to use some regex that will wrap uncoded urls (ie, someone has posted a url, but forgotten to put tags around it) with [url] tags. So b ... nd without after it.
Here's what I have so far:
Code: Select all
$code = preg_replace('/^(?!\[url(\=.*?)?\](\s)?)(http|https|ftp)\:\/\/+([a-z0-9\-\_\.]+)(\.[a-z]+){1,2}(.*?)($|\s)/', '[url]$3://$4$5[/url]$6', $code);
Code: Select all
<?php
function foo($code)
{
return preg_replace('/^(?!\[url(\=.*?)?\](\s)?)(http|https|ftp)\:\/\/+([a-z0-9\-\_\.]+)(\.[a-z]+){1,2}(.*?)($|\s)/', '[url]$3://$4$5[/url]$6', $code);
}
echo foo('http://google.co.uk').'<br />';
echo foo('[ url]http://google.co.uk[/url] (without the space)').'<br />';
echo foo('something before...so this should not work :( http://google.co.uk').'<br />';You (should) see that it works as long as there isn't anything before the url, IE, it works if the url is the beginning of the string. So, the third example won't work. I have a feeling it's something to do with the "^" at the beginning, but without that, it will perform the replacement even if the url is already wrapped with [url] tags.
So yeah...that's my dilemma. Any help would be greatly appreciated.
Thanks, all the best,
Jack.