0

I'm not very good at regular expressions at all.

What I am trying to do? I want to match all url's in a special string.

Basically, I want to match all urls to enclose them with an <a>- Tag, except the existing <a>- Tags.

For example, the following string should be matched:

Hallo! I am a text click here, that has a lot of urls www.aon.at?this=true and www.aon.at. all should be matched correctly http://www.aon.at and also working for aon.at/this?true

What should be matched:

Hallo! I am a text < a href='www.aon.at' >click here < /a >, that has a lot of urls www.aon.at?this=true and www.aon.at. all should be matched correctly http://www.aon.at and also working for aon.at/this?true

I have tried the regex from Linkify Regex Function PHP Daring Fireball Method

(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))

on the page https://regex101.com/ but it is not working the way I want it to do. As you can see, the regex is matching the <a>- Tag, and I don't know how to remove it.

enter image description here

Community
  • 1
  • 1
romaneso
  • 1,097
  • 1
  • 10
  • 26

1 Answers1

0

Nevermind, found the solution already.

With that solution, everything works fine

$pattern = '(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`\!()\[\]{};:\'".,<>?«»“”‘’]))';     
return preg_replace("!$pattern!i", "<a href=\"\\0\" rel=\"nofollow\" target=\"_blank\">\\0</a>", $str); 
romaneso
  • 1,097
  • 1
  • 10
  • 26