There is such a code, it works, but if there is <br /> , then particles from <br /> added to the url

 $new_text = preg_replace("~(http|https|ftp|ftps)://(.*?)(\s|\n|[,.?!](\s|\n)|$)~", '<a href="$1://$2">$1://$2</a>$3', $text); return $new_text; 

For example, the link https://www.google.com/ If it is written in the text like this

https://www.google.com/

That all works.

If it is written in the text like this

Find on Google - https://www.google.com/ Google Website

That all works.

If it is written in the text like this

Find in Google https://www.google.com/ <br/>
To Google

That link absorbs the particles.
and all that comes after her.

How to fix it? And is it possible to redirect from a link to a specific page in order to check whether this domain is allowed for the transition, etc.

Desirable with an explanation.

    1 answer 1

    Restrict angle brackets (and spaces) :

     $text = 'Найти в Google https://www.google.com/<br/>Загуглить'; $patt = '~(?:https?|ftps?)://[^<>\s]+~'; $repl = '<a href="$0">$0</a>'; $newText = preg_replace($patt, $repl, $text); echo $newText; 

    Result:

     Найти в Google <a href="https://www.google.com/">https://www.google.com/</a><br/>Загуглить