Dr. Logan | COM Courses | Syllabus | Reading Notes

Regular Expressions

Regular expression characters—table 4.4 (page 125):

  • Used outside square brackets:
    • \—escape character
    • ^—match at start of string
    • $—match at end of string
    • .—(dot) match any character except newline (\n)
    • |—start of alternative branch (means the same as "OR")
    • (—start subpattern
    • )—end subpattern
    • *—repeat zero or more times
    • {—start min/max quantifier
    • }—end min/max quantifier
    • ?—escape character
  • Used inside square brackets:
    • \—escape character
    • ^—NOT, only if used in first position
    • -—Used to specify character ranges
  • Others:
    • \d—Digit
    • \D—NOT a digit
    • \s—Whitespace
    • \S—NOT whitespace
  • Pattern match extenders
    • ?—Previous item is match 0 or 1 times
    • *—Repeat zero or more times
    • +—Repeat one or more times
    • {n}—Previous item is matched at least n times
    • {n,m}—Previous item is matched at least n times and at most m times
    • ? (after any of the 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

Here is a function that uses a regular expression to detect a zipcode, including one with a 4-digit extension:
<?php
// A function to detect zip codes 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);
}?>

Here are two calls, the first returning true, the second false:
//first example, returns true
if (detect_zipcode('Kingston, RI 02881-3016')){
echo "Test 1: true\n";
}
//second example, returns false
if (detect_zipcode('My phone number is 316-GL2-2419')) {
echo "Test 2: true\n";

Strings—General Functions

String Length: $length = strlen ($string);—returns number of characters. (strlen)

Split a string using a string as a separator: $new_array_of_strings = explode ($string_separator, $string);—returns array of substrings by splitting the string each time the string separator (e.g., a blank, comma, tab, etc.) occurs. (explode)

Combine strings together from an array: $big_string = implode ($separtor, $array_of_strings);—concatenates strings in the array, with a separator string (blanks, commas, etc.) between them. (implode)

Remove Spaces at beginning and end: $trimmed_string = trim ($string);—both ends. rtrim()—right end. ltrim()—left end. (trim) | (ltrim) | (rtrim)

Replace All Occurences of One String with Another String: $result = str_replace($old, $new, $whole_string);—take out the old and replace with the new. (str+replace)

Pad a string to a specific length: $padded = str_pad ($string, $length);—padded string is at least length characters; spaces added to the right. Optional characters can be used as padding, and it can be set to pad to the left. (see str_pad)

Create a longer string via replication: $repeat = str_repeat ($string, $nrepeats);—make a new string with $nrepeats repetitions of $string. (str_repeat)

Split array into characters: $array = str_split($string);— (str_split)

Find a Substring: $substring = substr ($string, $n, $length);—starting at the nth character, return the string that is $nlength characters long. (substr)

Change Case: $lower = strtolower ($string); or $upper = strtoupper ($string); (strtolower) | (strtoupper)

Parse based on a format: $array = sscanf ($string, $format);— (sscanf: " The function sscanf() is the input analog of printf(). sscanf() reads from the string str and interprets it according to the specified format, which is described in the documentation for sprintf().")

Output formatted values: printf ($format, $var1, $var2, ...);—format argument definces how arguments are printed. See (printf) | (sprintf) | (fprintf)

Compare Two Strings: $result = strcmp ($string1, $string2);—returns number < 0, =0, or > 0 depending on how the srings compare alphabetically; 0 if strings are equivalent. Case-insensitive version is strcasecmp(). (strcmp) | (strcasecmp)

Compare Two Strings as a Human would do: $result = strnatcmp ($string1, $string2);—strings with numbers in them are ordered based on value, not just characters. Case insensitve version is strnatcasecmp(). (strnatcmp) | (strnatcasecmp)