I am trying to run a string and find and replace the URLs with a link, this is what I have so far found and it seems that it works for the most part not bad, however there are a few things that I do. I like polishing. Also, this may not be the most efficient way to do this.
I read a lot of threads on this here on SO, and although it helped a lot, I still need to tie loose ends to it.
I run the line two times. The first time I replace bbtags with html tags; and the second time I run the line and replace the urls with text:
$body_str = preg_replace('/\[url=(.+?)\](.+?)\[\/url\]/i', '<a href="\1" rel="nofollow" target="_blank">\2</a>', $body_str); $body_str = preg_replace_callback( '!(?:^|[^"\'])(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?!', function ($matches) { return strpos(trim($matches[0]), 'thisone.com') == FALSE ? '<a href="' . ltrim($matches[0], " \t\n\r\0\x0B.,@?^=%&:/~\+#'") . '" rel="nofollow" target="_blank">' . ltrim($matches[0], "\t\n\r\0\x0B.,@?^=%&:/~\+#'") . '</a>' : '<a href="' . ltrim($matches[0], " \t\n\r\0\x0B.,@?^=%&:/~\+#'") . '">' . ltrim($matches[0], "\t\n\r\0\x0B.,@?^=%&:/~\+#'") . '</a>'; }, $body_str );
So far, a few problems that I find with this are, as a rule, picking up a character immediately before "http", etc., for example. space / comma / colon etc. which broke the links. So I used preg_replace_callback to get around this and trim some unwanted characters that would break the link.
Another problem is to avoid breaking links by matching URLs that are already in A tags, I currently exclude URLs starting with quotation, double quotation marks, and I would rather use href = '| href = for an exception.
Any tips and tricks would be much appreciated