To best demonstrate the usefulness of regular expressions, let's examine a relatively simple problem: searching for a United States ZIP Code. Such numbers are either a five-digit number, or are known as a ZIP+4 and look like "12345-6789." Attempting this search with the standard PHP string functions would require a multiline algorithm. However, with regular expressions, a single function, preg_match(), is all that is required, here's given example :
// A function to detect zipcodes in a string.
function detect_zipcode($string) {
// Use regex to look for zipcodes, return true or false
return preg_match('/\b\d{5}(-\d{4})?\b/', $string);
}
// Try a few examples:
echo '
';';
// A true example:
if (detect_zipcode('Frederick, MD 21701-3883')) {
echo "Test 1: true\n";
}
// Another true example:
if (detect_zipcode('The zipcode 26623 is the area in which I grew up.')) {
echo "Test 2: true\n";
}
// A False example:
if (detect_zipcode('The Phone Number is 301-555-1212')) {
echo "Test 3: true\n";
}
// Another false example:
if (detect_zipcode('42696-313109')) {
echo "Test 4: true\n";
}
echo '
?>
OUTPUT:
As a quick reference, here are some of the most common syntax characters for use in PCRE regular expressions:
Pattern matches:
\d = Digit
\D = Not a digit
\s = Whitespace
\S = Not whitespace
. = Any character (except \n)
^ = Start of string
$ = End of string
\b = Word boundary
Pattern match extenders:
? = Previous item is match 0 or 1 times.
* = Previous item is matched 0 or more times.
+ = Previous item is matched 1 or more times.
{n} = Previous item is matched exactly n times.
{n,} = Previous item is matched at least n times.
{n,m} = Previous item is matched at least n and at most m times.
? (after any of above) = Match as few as possible times.
Option patterns:
Cleaning Up Whitespace...::--
(pattern) = Groups the pattern to act as one item and captures it
(x|y) = Matches either pattern x, or pattern y
[abc] = Matches either the character a, b, or c
[^abc] = Matches any character except a, b, or c
[a-f] = Matches characters a through f
If you want to learn more try to read http://php.net/pcre.
If you want to just remove space from the beginning or end of a string, you can use the built-in function trim() to do that for you. However, often you want to completely clean up the data. You will want to remove leading/trailing spaces, collapse multiple spaces into a single one, and even replace all other types of whitespace with a regular space.
To accomplish this, you can use the regular expression functions of PHP as like as below:
$str = " This line contains\tliberal \r\n use of whitespace.\n\n";
// First remove the leading/trailing whitespace
$str = trim($str);
// Now remove any doubled-up whitespace
$str = preg_replace('/\s(?=\s)/', '', $str);
// Finally, replace any non-space whitespace, with a space
$str = preg_replace('/[\n\r\t]/', ' ', $str);
// Echo out: 'This line contains liberal use of whitespace.'
echo "{$str}";
?>
OUTPUT:
This line contains liberal use of whitespace.
|
No comments:
Post a Comment