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’;
}