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

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.