Saturday, June 26, 2010

@@String::::....Matching Patterns (Regular Expressions)

PHP includes a couple of different sets of regex functions that you can use. The one we will use exclusively here is the PCRE extension. The PCRE functions (all prefaced by preg_) is the preferred library to use. It tends to be faster than the other option, and it uses the same regex syntax as Perl, another popular programming language that many programmers are already familiar with.

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:

(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.

Cleaning Up Whitespace...::--

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.

Section 1.1. Matching Patterns (Regular Expressions)


facebook

International movers in newyork, Long distance movers in newyork, Automobile movers in newyork , Temporary storage, Moving supplies, moving overseas, international shipping, shipping, free quotes, moving quotes, moving company, international removals, removal company.Please visit here: baileysworldwide.com ====================================================== debris removal in nyc, garbage removal in nyc, debris removal in queens, demolition removal in queens, garbage removal in queens, garbage removal in New York.Please visit here : Richmond HILL demolition & garbage removal ======================================================= NYC work permit, NYC violation removal, Alt I, Alt II, Alt III filings,(interior, commercial build outs), New Building, Builders Pavement Plan (BPP), Sign applications, Sprinkler Fire alarm applications, Place of Assembly, Temporary Place of Assembly, Stop Work Order Removal. Please visit here : chameleonexpediting.com