Regular Expressions
Regular expression characters—table 4.4 (page 125):
|
|
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)