Pedro Paulo Pedro Paulo - 1 year ago 62
PHP Question

Regex extract URL without hyperlink

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


<a href="">Google</a>,,

I would have to get only:


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

Answer Source

You could use this RegEx:


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)