Question

OK, I've been using the following code to find any URLS in the text that I enter, but some things like YouTube code or whatnot have links in them and it finds those as well. I would like to have this code modified so that it ignores quotes around the links that are already a part of something.

`public function auto_link_text($text) {

    $pattern  = '#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#';
    return preg_replace_callback($pattern, array($this, 'auto_link_text_callback'), $text);

}`
Was it helpful?

Solution

This just excludes links preceded by a quotation mark

/(?<=[^"]|^)(https?|ftp|file):\/\/[-a-zA-Z0-9+&@#\/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#\/%=~_|]/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top