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
Q

Define and Show: strrchr()

A

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
Q

Define and Show: stristr()

A

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
Q

Define and Show: strpos()

A

Same as starts() but strips() is recommended because it runs faster.

$test = "Hello world";
echo strpos($test, "o");
28
Q

Define and Show: str_replace()

A

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
Q

Define and Show: substr_replace()

A

Finds and replaces a particular substring of a string based on its position.

$test = substr_replace($test, ‘X’, -1);

30
Q

Define and Show: ereg() and eregi()

A

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
Q

Define and Show: ereg_replace() and eregi_replace()

A

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
Q

Define and Show: split()

A

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;
}
33
Q

Define: \ (backslash)

A

Escape Character.

34
Q

Define: ˆ (carrot) outside square brackets?

A

Match at start of string.

35
Q

Define: $ (dollar sign)

A

Match at end of string.

36
Q

Define: . (period)

A

Match any character except \n (new line).

37
Q

Define: | (pipe)

A

Start of alternative branch (read as OR).

38
Q

Define: ( (front parenthesis)

A

Start subpattern.

39
Q

Define: ) (end parenthesis)

A

End subpattern.

40
Q

Define: * (asterisk)

A

Repeat zero or more times.

41
Q

Define: + (plus sign)

A

Repeat one or more times.

42
Q

Define: { (front curly brace)

A

Start min/max quantifier.

43
Q

Define: } (end curly brace)

A

End min/max quantifier.

44
Q

Define: ? (question mark)

A

Mark a subpattern as optional.

45
Q

Define: ˆ (carrot) inside square brackets?

A

NOT, only if used in initial position.

46
Q

Define: - (dash)

A

Used to specify character strings.

47
Q

Define: [[:alnum:]]

A

Alphanumeric characters

48
Q

Define: [[:alpha:]]

A

Alphabetic characters

49
Q

Define: [[:lower:]]

A

Lowercase letters

50
Q

Define: [[:upper:]]

A

Uppercase letters

51
Q

Define: [[:digit:]]

A

Decimal digits

52
Q

Define: [[:xdigit:]]

A

Hexadecimal digits

53
Q

Define: [[:punct:]]

A

Punctuation

54
Q

Define: [[:blank:]]

A

Tabs and spaces

55
Q

Define: [[:space:]]

A

Whitespace characters

56
Q

Define: [[:cntrl:]]

A

Control characters

57
Q

Define: [[:print:]]

A

All printable characters

58
Q

Define: [[:graph:]]

A

All printable characters except for space