Chapter 4 - String Manipulation and Regular Expressions Flashcards
Define and Show: isset()
Used to check that users have filled out all the required form fields.
if (!isset($name)) {
echo “You need to go back and write your name.”;
}
Define and Show: mail()
Used to set up your PHP installation to point at your mail sending program.
mail($toaddress, $subject, $mailcontent, $fromaddress);
Define and Show: ltrim()
Used to remove the whitespace on the left side of the string.
$name = ltrim($_POST[‘name’]);
Define and Show: rtrim()
Used to remove the whitespace on the right side of the string.
$name = rtrim($_POST[‘name’]);
Define and Show: trim()
Used to remove the whitespace from the start and end of the string.
$name = trim($_POST[‘name’]);
Define and Show: nl2br()
Takes a string as a parameter and replaces all the newlines with an XHTML <br></br> tag.
echo nl2br($mailcontent);
Define and Show: print()
Does the same this as echo, which is it prints the string to the screen, but with print() it returns true or false.
print(“Hello my name is Rob”);
Define and Show: printf()
Prints a formatted string to the browser.
printf(“Hello my name is Rob”);
Define and Show: sprintf()
Returns a formatted string.
sprintf(“Hello my name is Rob”);
Define and Show: %s
Replace with string (the s means string).
Other parameters: b: binary number c: character d: decimal number f: floating-point number o: octal number s: string u: unsigned decimal x: hexadecimal number with lowercase letters for the digits a-f. X: hexadecimal number with uppercase letters for the digits A-F.
printf(“Total amount of order is %.2f”, $total);
Define and Show: vprintf() & vsprintf()
These are a variant of printf() and sprintf(). They accept two parameters: the format string and an array of the arguments.
vprintf(“You win!”, $reward); // prints to the browser
vsprintf(“You win!”, $reward); // returns a formatted string
Define and Show: strtoupper()
Turns string to uppercase.
$uppercaseName = strtoupper($_POST[‘name’]);
Define and Show: strtolower()
Turns string to lowercase.
$lowercaseName = strtolower($_POST[‘name’]);
Define and Show: ucfirst()
Capitalizes first character of string if it’s alphabetic.
$name = ucfirst($_POST[‘name’]);
Define and Show: ucwords()
Capitalizes first character of each word in the string that begins with an alphabetic character.
$name = ucwords($_POST[‘name’]);
Define and Show: addslashes()
Takes a string parameter and returns the reformatted string. In this case, the slashes would be added to the string.
$feedback = addslashes(trim($_POST[‘feedback’’]));
Define and Show: stripslashes()
Takes a string parameter and returns the reformatted string. In this case, the slashes would be stripped from the string.
$feedback = stripslashes(trim($_POST[‘feedback’’]));
Define and Show: explode()
Takes a string input and splits it into pieces on a specified separator string all at one time.
$email_array = explode(‘@’, $email);
You can now use it for something like:
if ($email_array[1] == “bigcustomer.com”) {
$toaddress = “bob@example.com”;
} else {
$toaddress = “feedback@example.com”;
}
Define and Show: implode() or join()
They are the same. They join the bits of string together.
$new_email = implode(‘@’, $email_array);
Define and Show: strtok();
Similar to explode(), but strtok() get pieces (tokens) from a string one at a time.
$feedback = “Thanks for everything.”;
$token = strtok($feedback, “ “);
// This only finds the first separator then stops echo $token."<br />";
// This separates everything in the string while ($token != "") { $token = strtok(" "); echo $token."<br />"; }
Define and Show: substr();
Enables you to access a substring between a given start and end points of a string.
$test = ‘Your customer service is excellent’;
substr($test, 1);
Returns: our customer service is excellent
or
substr($test, 1, 13);
would return: our customer
Define and Show: strlen()
Used to check the length of a string.
echo strlen(“hello”);
Define and Show: strstr()
Used to find a string or character match within a longer string.
$feedback = “I really liked your shop.”;
if (strstr($feedback, ‘shop’))
$toaddress = ‘retail@example.com’;
}
Define and Show: strchr()
Used to find a character match within a longer string. Finds the first occurrence and return the rest of the string.
$feedback = "I really liked your shop."; echo strchr($feedback, 'sh');
Define and Show: strrchr()
Used to find a character match within a longer string. Finds the LAST occurrence and return the rest of the string. This IS case sensitive;
$feedback = "I really liked your shop."; echo strchr($feedback, 'r');
Define and Show: stristr()
Used to find a character match within a longer string. Finds the LAST occurrence and return the rest of the string. This is NOT case sensitive;
$feedback = "I really liked your shop."; echo strchr($feedback, 'r');
Define and Show: strpos()
Same as starts() but strips() is recommended because it runs faster.
$test = "Hello world"; echo strpos($test, "o");
Define and Show: str_replace()
Replaces all the instances of needle from haystack with new_needle and returns the new version of the haystack. The optional fourth parameter, count, contains the number of replacements made.
$roadRage = "You stupid mother fucker."; $bad = "stupid mother fucker.";
$test = str_replace($bad, “sweet person you!”, $roadRage);
echo $test;
Define and Show: substr_replace()
Finds and replaces a particular substring of a string based on its position.
$test = substr_replace($test, ‘X’, -1);
Define and Show: ereg() and eregi()
It searches the search string, looking for matches to the regular expression in pattern.
eregi() is the exact same function but is NOT case sensitive.
if (eregi(“deliver | fulfill”, $feedback)) {
$toaddress = “shipping@example.com
}
Define and Show: ereg_replace() and eregi_replace()
This function searches for the regular expression pattern in the search string and replace it with the string replacement.
eregi_replace() is the exact same function but is NOT case sensitive.
$pattern = 'delivery'; $replacement = 'shipping'; $body = eregi_replace($pattern, $replacement, $body);
Define and Show: split()
This function splits the search string into substrings on the regular expression pattern and returns the substrings in an array.
$address = "username@example.com"; $arr = split("\.|@", $address); while (list($key, $value) = each ($arr)) { echo "<br />".$value; }
Define: \ (backslash)
Escape Character.
Define: ˆ (carrot) outside square brackets?
Match at start of string.
Define: $ (dollar sign)
Match at end of string.
Define: . (period)
Match any character except \n (new line).
Define: | (pipe)
Start of alternative branch (read as OR).
Define: ( (front parenthesis)
Start subpattern.
Define: ) (end parenthesis)
End subpattern.
Define: * (asterisk)
Repeat zero or more times.
Define: + (plus sign)
Repeat one or more times.
Define: { (front curly brace)
Start min/max quantifier.
Define: } (end curly brace)
End min/max quantifier.
Define: ? (question mark)
Mark a subpattern as optional.
Define: ˆ (carrot) inside square brackets?
NOT, only if used in initial position.
Define: - (dash)
Used to specify character strings.
Define: [[:alnum:]]
Alphanumeric characters
Define: [[:alpha:]]
Alphabetic characters
Define: [[:lower:]]
Lowercase letters
Define: [[:upper:]]
Uppercase letters
Define: [[:digit:]]
Decimal digits
Define: [[:xdigit:]]
Hexadecimal digits
Define: [[:punct:]]
Punctuation
Define: [[:blank:]]
Tabs and spaces
Define: [[:space:]]
Whitespace characters
Define: [[:cntrl:]]
Control characters
Define: [[:print:]]
All printable characters
Define: [[:graph:]]
All printable characters except for space