3

filter_var($url, FILTER_VALIDATE_URL) seems to be great way to detect if $url contains url or not. Is there any way to see what regular expression this function uses for detecting?

Thank you

Kirzilla
  • 16,368
  • 26
  • 84
  • 129

2 Answers2

6

It uses something else then a regex. In C, it checks the return of the php_url_parse_ex()(C) function, which you can see at: ext/standard/url.c, line 97, called at ext/filter/logical_filters.c, line 440.

In these terms: if you call parse_url()(PHP) in PHP, and perform the same checks as in php_filter_validate_url()(C), you'd have the same output.

Artefacto
  • 96,375
  • 17
  • 202
  • 225
Wrikken
  • 69,272
  • 8
  • 97
  • 136
0

The pattern is similar to the URL parsing.

$pattern = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i";
Nick Presta
  • 28,134
  • 6
  • 57
  • 76
Elixir Techne
  • 1,848
  • 15
  • 20