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 Expressions
PHP supports two different types of regular expressions:
- POSIX-extended
- Perl-Compatible Regular Expressions (PCRE) - more
powerful and faster than the POSIX
- emulate the Perl syntax for patterns
- each pattern must be enclosed in a pair of delimiters usually the
slash (/) character
For example: /pattern/ - PCRE functions can be divided in several classes: matching, replacing, splitting and filtering.
The Basics
In a regular expression, most characters match only themselves. For instance, if you search for the regular expression "base" in the string "Alex plays baseball," you get a match because "base" occurs in that string.
Character Class
A character class lets you represent several characters as a single item in a regular expression.
Build your own character class - by enclosing the acceptable characters in square brackets [ ]. A character class matches any one of the characters in the class.
- [abc] - matches a, b or c.
- [a-zA-Z0-9] - matches all lowercase letters a-z, uppercase letters A-Z and all digits 0-9
- [^0-9] - (negated character class) matches any character that is not in the class.
Special Meanings or Metacharacters
The characters that match themselves are called literals. If you want to match a literal metacharacter in a pattern, you have to escape it with a \ (backslash). In PHP3, you must escape the backslash character itself. For example: "(\$|¥)[0-9]+" would be "(\\$|¥)[0-9]+"
- $ - used to match strings that end with the given pattern.
- ^ (caret) - at the beginning of a regular expression indicates that it must match the beginning of the string.
- ^ (caret) as the first symbol in a bracket expression - used as NOT or characters you DON'T want
- . (period) -
matches any single character except newline (\).
For example: pattern h.t matches hat, hothit, hut, h7t - | ( vertical pipe) - behaves much like a logical OR operator and used to
match more than one set of characters
For exampe: Utah|Idaho|Nevada matches strings that contain "Utah" or "Idaho" or "Nevada". - ( ) (parentheses) - a group sequences
For example: (Nant|b)ucket matches "Nantucket" or "bucket". Using parentheses to group together characters for alternation is called grouping.
- Back references make it possible to use part of a matched
pattern in the replacement string.
- ( ) use parentheses to wrap any elements of your regular expression that you might want to use.
- $ refer to the text matched by subpattern with a dollar sign ($) and
the number of the subpattern. For instance, if you are using subpatterns,
- $0 is set to the whole match
- $1, $2, and so on are set to the individual matches for each subpattern.
- +, *, ?, and { } - affect the number of times a pattern
should be matched.
- + match one or more of the preceding expression
- * match zero or more of the preceding expression
searcharray
Array ( [0] => /ab+/ [1] => /\<a (.+=("|\')?.+("|\')?)*\>/ [2] => /picnic/ )
replace
Array ( [0] => XXXXX )
New data:matches
- ? match zero or one of the preceding expression
- { } can be
used differently. With a single integer,
- {n} match exactly n occurrences of the preceding expression
- {n,} match n or more occurrences of the preceding expression
- {n,m} match the previous character if it occurs at least n times, but no more than m times
- [ ] (bracket expressions) - Character Class - matches a single character out of all the possibilities offered by the character class.
Regular Expression | Matches |
---|---|
word | a string that has the text "word" in it. |
^word | any string that starts with "word" |
the word$ | a string that ends in the substring "the word"; |
^word$ | "word" when it is alone on a string |
ab* | matches a string that has an a followed by zero
or more b's "a", "ab", "abbb" |
ab+ | a string that has an a followed by at least
one b "ab", "abbb" |
ab? | the letter a followed by zero or one b |
a?b+$ | zero or one a followed by one or more b's ending a string |
ab{2} | matches a string that has an a followed by exactly
two b's "abb" |
ab{2,} | a followed by at least two b's "abb", "abbbb" |
ab{3,5} | a followed by three to five b's "abbb", "abbbb", or "abbbbb" |
a(bc)* | matches a followed by zero or more copies of the sequence "bc" |
a(bc){1,5} | a followed by one through five copies of "bc." |
[abc] | a, b, or c |
[a-z] | Any lowercase letter |
[^A-Z] | Any character that is not a uppercase letter |
a.[0-9] | a followed by one character and a digit |
^.{3}$ | exactly 3 characters |
(gif|jpg) | Matches either "gif" or "jpeg" |
(b|he)ad | either "bad" or "head" |
(a|b)*c | string that has a sequence of alternating a's and b's ending in a c |
[a-z]+ | One or more lowercase letters |
[0-9\.\-] | Аny number, dot, or minus sign NOTICE: the dot and minus are escaped |
[0-9]% | a single digit before a percent 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 |
%[^a-zA-Z]% | matches a string with a character that is not a letter between two percent signs |
Anchors
- ^ start of line
- \A Start of string
- $ end of line
- \Z End of string
- \b word boundary
- \B not work broundary
- \< start of word
- \> end of word
<?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"
<?php
$regexp="/\btest\b/i";
$replace=" *** ";
$string="This is a test of the word boundary to see if the testing works.";
echo preg_replace ( $regexp, $replace, $string);
?>
In the example above, only the word test was replace and testing was not replaced.
The preg_match() function performs Perl-style pattern matching on a string. preg_match() takes two basic and three optional parameters. These parameters are, in order, a regular expression string, a source string, an array variable which stores matches, a flag argument and an offset parameter that can be used to specify the alternate place from which to start the search:
preg_match ( pattern, subject [, matches [, flags [, offset]]])
The preg_match() function returns 1 if a match is found and 0 otherwise. Let's search the string "Hello World!" for the letters "ll":
<?php
if (preg_match("/ell/", "Hello World!", $matches)) {
echo "Match was found <br />";
echo $matches[0];
}
?>
The letters "ll" exist in "Hello", so preg_match() returns 1 and the first element of the $matches variable is filled with the string that matched the pattern. The regular expression in the next example is looking for the letters "ell", but looking for them with following characters:
<?php
if (preg_match("/ll.*/", "The History of Halloween", $matches))
{
echo "Match was found <br />";
echo $matches[0];
}
?>
Check Passwords
Checks if the password is "strong", i.e. the password must be at least 8 characters and must contain at least one lower case letter, one upper case letter and one digit:
<?php
$password = "Fyfjk34sdfjfsjq7";
$password = trim($password); //Trim space off front and end of password
if ((!preg_match("/s/",$dPwd))&&(preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/",
$password))) {
echo "Your passwords is strong.";
} else
{
echo "Your password is weak.";
}
?>
- ^ start and the end of the string
- .* metacharacter - any alphanumeric character zero or more times
- Groupings in parentheses
- .?= combination means "the next text must be like this". This construct doesn't capture the text. In this example, instead of specifying the order that things should appear, it's saying that it must appear but we're not worried about the order.
- grouping is (?=.*{8,}) - This checks if there are at least 8 characters in the string.
- grouping (?=.*[0-9]) means "any alphanumeric character can happen zero or more times, then any digit can happen. So this checks if there is at least one number in the string. But since the string isn't captured, that one digit can appear anywhere in the string.
- groupings (?=.*[a-z]) looking for the lower case letter anywhere in the string
- grouping (?=.*[A-Z]) looking for the upper case letter anywhere in the string
Validates an email address:
<?php
$email = firstname.lastname@aaa.bbb.com;
$regexp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/";
if (preg_match($regexp, $email)) {
echo "Email address is valid.";
} else {
echo "Email address is <u>not</u> valid.";
}
?>
This regular expression checks for the number at the beginning and also checks for multiple periods in the user name and domain name in the email address.
For the speed reasons, the preg_match() function matches only the first pattern it finds in a string. This means it is very quick to check whether a pattern exists in a string. An alternative function, preg_match_all(), matches a pattern against a string as many times as the pattern allows, and returns the number of times it matched.
In the above examples, we have searched for patterns in a string, leaving the search string untouched.
preg_replace()
The preg_replace function is used to do a find and replace on a string/array. We can give it one thing to find and replace (for example it seeks out the word 'him' and changes it to 'her') or we can give it a full list of things (an array) to search for, each with a corresponding replacement. It is phrased as
preg_replace ( search_for, replace_with, your_data , optional_limit, optional_count )
The limit will default to -1 which is no limit. Remember your_data can be a string or an array.
The preg_replace() function looks for substrings that match a pattern and replaces them with new text. preg_replace() takes three basic parameters and an additional one. These parameters are, in order, a regular expression, the text with which to replace a found pattern, the string to modify, and the last optional argument which specifies how many matches will be replaced.
preg_replace( pattern, replacement, subject [, limit ])
The function returns the changed string if a match was found or an unchanged copy of the original string otherwise. In the following example we search for the copyright phrase and replace the year with the current. Back Reference is used in the replacement string.
<?php
echo preg_replace("/([Cc]opyright) 200(3|4|5|6)/", "$1 2007", "Copyright
2005");
?>
This example will find the word copyright and year and replace it: Copyright 2007
Change the Date Format
Uses Back Reference to change the date from "yyyy-mm-dd" to "mm/dd/yyy".
<?php
echo preg_replace("/(\d+)-(\d+)-(\d+)/", "$2/$3/$1", "2007-01-25");
?>
Using Arrays and Pattern Modifier
Pass an array of patterns and and array of replacements to replace multiple items in a string or array of strings using one preg_replace().
<?php
$searcharray = array ( "/(\w{6}\s\(w{2})\s(\w+)/e",
"/(\d{4})-(\d{2})-(\d{2})\s(\d{2}:\d{2}:\d{2})/");
$replacearray = array ('"$1 ".strtoupper("$2")',
"$3/$2/$1 $4");
$string = "Posted by John | 2007-02-15 02:43:41";
echo preg_replace($searcharray, $replacearray, $string);?>
Pattern Modifier - using the e pattern modifier at the end of the search array tells PHP that the match text should be executed as PHP code once the replacement has taken place. It will take strtoupper(name) and replace it with the result of the strtoupper() function, which is NAME.
preg_split()
The preg_split() function enables you to break a string apart basing on something more complicated than a literal sequence of characters. When it's necessary to split a string with a dynamic expression rather than a fixed one, this function comes to the rescue. It returns an array of items that did NOT match the pattern.
<?php
/*************************
* [ ] Character Class
* \s looks for white space
* , looks for comma(s)
* + match one or more of the preceding expression
*************************/
$search ="/[\s,]+/";
$keywords_array = preg_split($search , "Hello, this is my test of preg_split
$10,000");
//**************Print Array ************************
echo "<h1>keywords_array</h1><pre>".htmlspecialchars(print_r($keywords_array,
true))."</pre>";
//**************************************************
?>
keywords_array
Array ( [0] => Hello [1] => this [2] => is [3] => my [4] => test [5] => of [6] => preg_split [7] => $10 [8] => 000 )
preg_grep() function
The preg_grep() function returns those elements of an array that match a given pattern. This function traverses the input array, testing all elements against the supplied pattern. If a match is found, the matching element is returned as part of the array containing all matches. The following example searches through an array and all the names starting with letters A-J:
<?php
$names = array('Andrew','John','Peter','Nastin','Bill');
$output = preg_grep('/^[a-m]/i', $names);
print_r( $output );
?>
Page last updated: May 31, 2012 10:46 AM
Content and Navigation...