GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing regexone.com — RegexOne - Learn Regular Expressions - PHP All Lessons
Lesson 1: An Introduction, and the ABCs
Lesson 1½: The 123s
Lesson 2: The Dot
Lesson 3: Matching specific characters
Lesson 4: Excluding specific characters
Lesson 5: Character ranges
Lesson 6: Catching some zzz's
Lesson 7: Mr. Kleene, Mr. Kleene
Lesson 8: Characters optional
Lesson 9: All this whitespace
Lesson 10: Starting and ending
Lesson 11: Match groups
Lesson 12: Nested groups
Lesson 13: More group work
Lesson 14: It's all conditional
Lesson 15: Other special characters
Lesson X: Infinity and beyond!
Practice Problems
Problem 1: Matching a decimal numbers
Problem 2: Matching phone numbers
Problem 3: Matching emails
Problem 4: Matching HTML
Problem 5: Matching specific filenames
Problem 6: Trimming whitespace from start and end of line
Problem 7: Extracting information from a log file
Problem 8: Parsing and extracting data from a URL
Problem X: Infinity and beyond!
Using Regular Expressions in PHP
If you need a refresher on how Regular Expressions work, check out our Interactive Tutorial first! PHP supports regular expressions through the use of the PCRE (Perl Compatible Regular Expressions) library which is enabled in almost all PHP installations. When calling any of the methods below, PHP requires that each pattern starts and ends with the same delimiter to differentiate it from a normal string, with the most common delimiter being the forward slash character. For example, you would write the regular expression Matching a stringIn PHP, you can use the Method
$has_matches = preg_match($pattern, $input_str, $matches_out)
Example
// Lets use a regular expression to match a date string. Ignore
// the output since we are just testing if the regex matches.
$regex = "/[a-zA-Z]+ \d+/";
if (preg_match($regex, "June 24")) {
// Indeed, the expression "[a-zA-Z]+ \d+" matches the date string
echo "Found a match!";
} else {
// If preg_match() returns false, then the regex does not
// match the string
echo "The regex pattern does not match. :(";
}
Capturing groupsUnlike the Method
$has_matches = preg_match_all($pattern, $input_str, $matches_out)
Example
// Lets write a regular expression to extract the day of month in
// a string with numerous dates
$pattern = "/[a-zA-Z]+ (\d+)/";
$input_str = "June 24, August 13, and December 30";
if (preg_match_all($pattern, $input_str, $matches_out)) {
// $matches_out is now an Array of size equal to N+1, for N
// number of groups you are capturing in your regex, and +1
// for the substrings that matched.
// This will print "2" because we are capturing only one group
echo count($matches_out);
// In addition, each value in $matches_out is another Array of
// size M, for M number of matches of the regex in the input.
// This will print "3" for the three dates in our input string
echo count($matches_out[0]);
// $matches_out[0] is an Array of the matched strings from the
// input string.
// This prints an Array ("June 24", "August 13", "December 30")
print_r($matches_out[0]);
// $matches_out[1], $matches_out[2], etc. are Arrays filled with
// the captured data in the same order as in the regex pattern.
// This prints an Array ("24", "13", "30")
print_r($matches_out[1]);
}
Finding and replacing stringsIt is handy sometimes to find and replace a string using regular expressions, for example, to
replace all instances of an old email domain with a new one, or to swap the order of some text. You
can do this with the Method
$replaced_str = preg_replace($pattern, $partial_replacement_str, $input_str)
Example
// Lets try and reverse the order of the day and month in a date
// string. Notice how we can reference the two groups directly
// by index (ie. $1 and $2).
$regex = "/([a-zA-Z]+) (\d+)/";
$new_string = preg_replace($regex, "$2 of $1", "June 24");
// The string returned is either the same input string if the
// regex does not match, or the transformed string.
// This prints "24 of June"
echo $new_string;
LinksFor more information about using regular expressions in PHP, please visit the following links: Lesson Notes
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |