NOTE: This is a collection of information and links collected over the years that might provide useful information. A Safer Company LLC does not guarantee, endorse or approve any of these links or their scripts. Use these links to other websites at your own risk.
- Web Design - IP Addresses
- cookie.txt
- Cookies and Microsoft Vista
- Accept Credit Cards
- FavIcon
- Translate into Other Languages
- Game Development and 3D
- .htaccess
- HTML - Conditional Comments for IE
- Icons
- Advanced Programming
- regular-expressions-cheat-sheet-v2.pdf
- Regular Expression- Regex
- Regular Expressions
- Preg_Replace PHP Function
- SSI - Server Side ludes
- Web Development Tools
Regular Expression - Regex
A regular expression is a pattern describing a certain amount of text. It is also called regex.
- A way of describing a pattern in text - for example:
- all the words that begin with the letter
- every 10-digit phone number
- every sentence with two commas in it, and no capital letter Q
Check for a phone number pattern:
/^([1]-)? [0-9]{3}- [0-9]{3}- [0-9]{4} $/i
Check for email pattern:
/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i
- amount of letters or numbers
- followed by the @ symbol
- followed by any letters or numbers (the domain name)
- followed by a .
- and then ending with any combination of 2 or 3 letters
Find a Phrase in a Phrase
<?php // The "i" after the pattern delimiter indicates a case-insensitive search if (preg_match("/php/i", "PHP is the web scripting language of choice.")) { echo "A match was found."; } else { echo "A match was not found."; } ?>
Find a Word in a Phrase
<?php /* The \b in the pattern indicates a word boundary, so only the distinct word "web" is matched, and not a word partial like "webbing" or "cobweb" */ if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) { echo "A match was found."; } else { echo "A match was not found."; }
Using named subpattern
<?php $str = 'foobar: 2008'; preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches); /* This also works in PHP 5.2.2 (PCRE 7.0) and later, however * the above form is recommended for backwards compatibility */ // preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches); print_r($matches); ?>
The above example will output:
Array ( [0] => foobar: 2008 [name] => foobar [1] => foobar [digit] => 2008 [2] => 2008 )
Learning Regex
- Meta-characters
- Regular Expression Tutorial - Learn How to Use and Get The Most out of Regular Expressions
- Web Cheat Sheet - Using Regular Expressions with PHP
- Wikipedia - Regular_expression
Links
- Regular Expression Basic Syntax Reference
- Sample Regular Expressions
- http://www.regular-expressions.info/regexbuddy/email.html
- Advanced regexps
- PHP preg_match Regular Expression (Regexp) Tester
Regular Expression | Will match... |
---|---|
foo | The string "foo" |
^foo | "foo" at the start of a string |
foo$ | "foo" at the end of a string |
^foo$ | "foo" when it is alone on a string |
[abc] | a, b, or c |
[a-z] | Any lowercase letter |
[^A-Z] | Any character that is not a uppercase letter |
(gif|jpg) | Matches either "gif" or "jpeg" |
[a-z]+ | One or more lowercase letters |
[0-9\.\-] | Аny number, dot, or minus sign |
^[a-zA-Z0-9_]{1,}$ | Any word of at least one letter, number or _ |
([wx])([yz]) | wy, wz, xy, or xz |
[^A-Za-z0-9] | Any symbol (not a number or a letter) |
([A-Z]{3}|[0-9]{4}) | Matches three letters or four numbers |
Page last updated: May 31, 2012 10:46 AM
Content and Navigation...