regular expression examples


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Matching a Valid Email Address
  2. view source
  3. print?
  4.  
  5. if (!preg_match('#[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{1,10}#i', $email)) {
  6.  
  7. echo 'The email address you entered was invalid.';
  8.  
  9. }
  10. Matching a string between 4 and 128 characters
  11. view source
  12. print?
  13. $string = "tutorial";
  14.  
  15. if (!preg_match('#^(.){4,128}$#', $string)) {
  16.  
  17. echo 'The string you entered is invalid.';
  18.  
  19. }
  20. Matching an integer between 1 and 16 characters in length
  21. view source
  22. print?
  23. $int = 4;
  24.  
  25. if (!preg_match('#^[0-9]{1,16}$#', $int)) {
  26.  
  27. echo 'That is not a valid integer.';
  28.  
  29. }
  30. Matching Content between any given HTML tag with attributes
  31.  
  32. simply replace �style� with HTML tag your trying to match.
  33. view source
  34. print?
  35. $html = '<style class="sdfgsd">this is the matched pattern</style>';
  36.  
  37. preg_match('/<style(.*)?>(.*)?<\/style>/', $html, $match);
  38.  
  39. print $match[2];
  40. Matching a Valid http:// URL
  41. view source
  42. print?
  43. $url = http://www.tutorialcadet.com
  44.  
  45. if (!preg_match('/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/', $url)) {
  46.  
  47. echo 'The URL you entered was invalid. Be sure to include <strong>http://</strong>';
  48.  
  49. }
  50. Matching a US Phone Number
  51. view source
  52. print?
  53. $phonenumber = '333-333-3333';
  54.  
  55. if(preg_match("/^[0-9]{3,3}[-]{1,1}[0-9]{3,3}[-]{1,1}[0-9]{4,4}$/", $phonenumber)) {
  56. echo $phonenumber;
  57. }
  58. Matching a UK Phone Number
  59. view source
  60. print?
  61. $ukphonenumber = '01614840484';
  62.  
  63. if(preg_match("/^[0-9]{11,11}$/", $ukphonenumber)) {
  64.  
  65. echo $ukphonenumber;
  66.  
  67. }
  68. Matching a UK Postal code
  69. view source
  70. print?
  71. $postal = 'AL42PT';
  72.  
  73. if(preg_match("/^[A-Z]{1,2}([0-9]{1,2}|[0-9]{1,1}[A-Z]{1,1})( |)[0-9]{1,1}[A-Z]{2,2}$/", $postal)) {
  74.  
  75. echo $postal;
  76.  
  77. }
  78. Matching a US Zip Code
  79. view source
  80. print?
  81. $zip = '55416';
  82.  
  83. if(preg_match("/^[0-9]{5,5}$/", $zip)) {
  84. echo $zip;
  85. }
  86. Matching an IPv4 IP address
  87. view source
  88. print?
  89. $ip = '233.122.122.255';
  90.  
  91. if(preg_match("/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/", $ip)) {
  92.  
  93. echo $ip;
  94.  
  95. }
  96. Matching an IPv6 IP address
  97. view source
  98. print?
  99. <?php
  100.  
  101. $ip = 'fe80:0000:0000:0000:0204:61ff:fe9d:f156';
  102.  
  103. if(preg_match("/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/", $ip)) {
  104. echo $ip;
  105. }
  106.  
  107. ?>

URL: http://www.tutorialcadet.com/10-useful-regex-patterns-for-php/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.