Chapter 4 - String Manipulation and Regular Expressions Flashcards

1
Q

Define and Show: isset()

A

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.”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define and Show: mail()

A

Used to set up your PHP installation to point at your mail sending program.

mail($toaddress, $subject, $mailcontent, $fromaddress);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define and Show: ltrim()

A

Used to remove the whitespace on the left side of the string.

$name = ltrim($_POST[‘name’]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define and Show: rtrim()

A

Used to remove the whitespace on the right side of the string.

$name = rtrim($_POST[‘name’]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define and Show: trim()

A

Used to remove the whitespace from the start and end of the string.

$name = trim($_POST[‘name’]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define and Show: nl2br()

A

Takes a string as a parameter and replaces all the newlines with an XHTML <br></br> tag.

echo nl2br($mailcontent);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define and Show: print()

A

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”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Define and Show: printf()

A

Prints a formatted string to the browser.

printf(“Hello my name is Rob”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Define and Show: sprintf()

A

Returns a formatted string.

sprintf(“Hello my name is Rob”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Define and Show: %s

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Define and Show: vprintf() & vsprintf()

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Define and Show: strtoupper()

A

Turns string to uppercase.

$uppercaseName = strtoupper($_POST[‘name’]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Define and Show: strtolower()

A

Turns string to lowercase.

$lowercaseName = strtolower($_POST[‘name’]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Define and Show: ucfirst()

A

Capitalizes first character of string if it’s alphabetic.

$name = ucfirst($_POST[‘name’]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Define and Show: ucwords()

A

Capitalizes first character of each word in the string that begins with an alphabetic character.

$name = ucwords($_POST[‘name’]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Define and Show: addslashes()

A

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’’]));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Define and Show: stripslashes()

A

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’’]));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Define and Show: explode()

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Define and Show: implode() or join()

A

They are the same. They join the bits of string together.

$new_email = implode(‘@’, $email_array);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Define and Show: strtok();

A

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 />"; 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Define and Show: substr();

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Define and Show: strlen()

A

Used to check the length of a string.

echo strlen(“hello”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Define and Show: strstr()

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Define and Show: strchr()

A

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');
25
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'); ```
26
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'); ```
27
Define and Show: strpos()
Same as starts() but strips() is recommended because it runs faster. ``` $test = "Hello world"; echo strpos($test, "o"); ```
28
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;
29
Define and Show: substr_replace()
Finds and replaces a particular substring of a string based on its position. $test = substr_replace($test, ‘X’, -1);
30
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 }
31
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); ```
32
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 "
".$value; } ```
33
Define: \ (backslash)
Escape Character.
34
Define: ˆ (carrot) outside square brackets?
Match at start of string.
35
Define: $ (dollar sign)
Match at end of string.
36
Define: . (period)
Match any character except \n (new line).
37
Define: | (pipe)
Start of alternative branch (read as OR).
38
Define: ( (front parenthesis)
Start subpattern.
39
Define: ) (end parenthesis)
End subpattern.
40
Define: * (asterisk)
Repeat zero or more times.
41
Define: + (plus sign)
Repeat one or more times.
42
Define: { (front curly brace)
Start min/max quantifier.
43
Define: } (end curly brace)
End min/max quantifier.
44
Define: ? (question mark)
Mark a subpattern as optional.
45
Define: ˆ (carrot) inside square brackets?
NOT, only if used in initial position.
46
Define: - (dash)
Used to specify character strings.
47
Define: [[:alnum:]]
Alphanumeric characters
48
Define: [[:alpha:]]
Alphabetic characters
49
Define: [[:lower:]]
Lowercase letters
50
Define: [[:upper:]]
Uppercase letters
51
Define: [[:digit:]]
Decimal digits
52
Define: [[:xdigit:]]
Hexadecimal digits
53
Define: [[:punct:]]
Punctuation
54
Define: [[:blank:]]
Tabs and spaces
55
Define: [[:space:]]
Whitespace characters
56
Define: [[:cntrl:]]
Control characters
57
Define: [[:print:]]
All printable characters
58
Define: [[:graph:]]
All printable characters except for space