Pedro Paulo Pedro Paulo - 7 months ago 29
PHP Question

Regex extract URL without hyperlink

I need to extract URL, but does not have the tag

Example

<a href="http://www.google.com">Google</a>, http://example.com, www.example.com


I would have to get only:

http:// example.com
www.example.com

Thanks for the help, I'm using Google Translator, sorry for the mistakes.

Answer

You could use this RegEx:

(?<!href=(?:"|'))((https?|ftp):\/\/(\w+\.)+(\w{1,3})(\/[^\s]*)?)

And extract the data from the 1st Capture Group

Live Demo on Regex101

How it works:

(?<!          # Negative Lookbehind to fail if inside a href of an <a> tag
  href=         # href=
  (?:"|')       # Opening ' OR "
)
(             # Capture URL (Capture Group #1)
  (https?|ftp)  # Protocol (HTTP, HTTPS or FTP)
  :\/\/         # ://
  (\w+\.)+      # Domain and Sub-Domains
  (\w{1,3})     # Final TLD (.com, .org, .uk)
  (             # Path after Website (Optional)
    \/          # /
    [^\s]*      # Any character except a [space] any number of times (including 0)
  )?
)