简体   繁体   中英

Regular expression pattern to match url with or without http(s) and without tags

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.

在此处输入图片说明

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM