Advertisement
Guest User

Regular Expression for validating URIs (PHP)

a guest
Jun 21st, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.85 KB | None | 0 0
  1. /**
  2.  * Regular expression for matching URIs
  3.  *
  4.  * Significantly more accurate than any other method I've come across,
  5.  * including the built in PHP methods.
  6.  *
  7.  * @author ChristianF
  8.  * @link www.fagsoft.no
  9.  * @licence http://creativecommons.org/licenses/by-sa/3.0/
  10.  */
  11. $RegExp = '#^(?:(?:(?:f|ht)tps?|dchub|sftp|steam)://)?'.
  12.     // Username-password combos.
  13.     '(?:\\w+(?::\\w+)?@)?'.
  14.  
  15.     // Domain or IP address
  16.     '(?:((?:[\\w\\pL][\\w\\pL-]*(?<!\\-)\\.)+[a-z\\pL]{2,5})(?::\\d{1,5})?'.
  17.     '|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))'.
  18.  
  19.     // URL Path
  20.     '((?:(?<!/)/(?:\w(?:%[a-f\\d]{2}|[\\w\\., -])*)*)+(?:(?:\\.\\w{1,6})?'.
  21.     // URL param
  22.     '(\\?(?:(?:%[a-f\\d]{2}|[\\w\\.-])+=(?:%[a-f\\d]{2}|[\\w\\.-])+)(?:&(?:%[a-f\\d]{2}|[\\w\\.-])+(?:=(?:%[a-f\\d]{2}|[\\w\\.-])+)?)*&?)?'.
  23.     ')?)?\\z#ui';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement