Thứ Bảy, 1 tháng 3, 2014

Tài liệu Zend PHP Certification Study Guide- P6 ppt

84
Chapter 4 Arrays
<?php
$a = array (‘a’ => 10, 20, 30, 40);
$b = array (‘a’ => 20, 20, 30, 40);
$array = array_merge_recursive ($a, $b);
var_dump ($array);
?>
This results in the following array:
array(7) {
[“a”]=>
array(2) {
[0]=>
int(10)
[1]=>
int(20)
}
[0]=>
int(20)
[1]=>
int(30)
[2]=>
int(40)
[3]=>
int(20)
[4]=>
int(30)
[5]=>
int(40)
}
In this case, $a[‘a’] and $b[‘a’] are combined together into the $array[‘a’] array.
Intersection and Difference
If you want to extract all the elements that are common to two or more arrays, you can
use the array_intersect():
<?php
$a = array (‘a’ => 20, 36, 40);
$b = array (‘b’ => 20, 30, 40);
05 7090 ch04 7/16/04 8:43 AM Page 84
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
85
Serializing Arrays
$array = array_intersect ($a, $b);
var_dump ($array);
?>
Here’s the output:
array(2) {
[“a”]=>
int(20)
[1]=>
int(40)
}
As you can see, this function only checks whether the values are the same—the keys are
ignored (although the key of the leftmost array is preserved). If you want to include
them in the check, you should use array_intersect_assoc() instead:
<?php
$a = array (‘a’ => 20, 36, 40);
$b = array (‘b’ => 20, 30, 40);
$array = array_intersect_assoc ($a, $b);
var_dump ($array);
?>
In this case, the result will be a one-element array because the two 20 values in $a and
$b have different keys:
array(1) {
[1]=>
int(40)
}
If you want to calculate the difference between two or more arrays—that is, elements
that only appear in one of the arrays but not in any of the others—you will need to use
either array_diff() or array_diff_assoc() instead.
Serializing Arrays
Given their flexibility, arrays are often used to store all sorts of information, and it is
handy to be able to save their contents at the end of a script and retrieve them later on.
This is done through a process, known as “serialization,” in which the contents of an
array are rendered in a format that can later be used to rebuild the array in memory.
05 7090 ch04 7/16/04 8:43 AM Page 85
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
86
Chapter 4 Arrays
In PHP, serialization is taken care of by two functions:
n
serialize() renders the array in a format that can be safely saved to any contain-
er (such as a database field or a file) capable of handling textual content.
n
unserialize() takes a serialized input and rebuilds the array in memory.
Using these two functions is very easy:
<?php
$a = array (‘a’ => 20, 36, 40);
$saved = serialize ($a);
// Your script may stop here if you save the contents
// of $saved in a file or database field
$restored = unserialize ($saved);
?>
The serialization functionality is very flexible and will be able to save everything that is
stored in your array—except, of course, for resource variables, which will have to be re-
created when the array is unserialized.
Exam Prep Questions
1. Which of the following types can be used as an array key? (Select three.)
A. Integer
B. Floating-point
C. Array
D. Object
E. Boolean
Answers A, B, and E are correct.A Boolean value will be converted to either 0 if
it is false or 1 if it is true, whereas a floating-point value will be truncated to its
integer equivalent. Arrays and objects, however, cannot be used under any circum-
stance.
05 7090 ch04 7/16/04 8:43 AM Page 86
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
87
Exam Prep Questions
2. Which of the following functions can be used to sort an array by its keys in
descending order?
A. sort
B. rsort
C. ksort
D. krsort
E. reverse_sort
D is correct.The sort() and rsort() functions operate on values, whereas
ksort() sorts in ascending order and reverse_sort() is not a PHP function.
3. What will the following script output?
<?php
$a = array (‘a’ => 20, 1 => 36, 40);
array_rand ($a);
echo $a[0];
?>
A. A random value from $a
B. ‘a’
C. 20
D. 36
E. Nothing
Only E is correct.The $a array doesn’t have any element with a numeric key of
zero, and the array_rand() function does not change the keys of the array’s ele-
ments—only their order.
Questions of this type are in the exam not to trick you, but rather as a way to test your
ability to troubleshoot a problem. In this particular example, a developer who is well
versed in PHP recognizes the problem immediately, whereas a less experienced program-
mer will be sidetracked by thinking that something is wrong with the function being
called. After all, these kinds of bugs, usually caused by distraction or typos, are quite com-
mon in real-life code.
05 7090 ch04 7/16/04 8:43 AM Page 87
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
05 7090 ch04 7/16/04 8:43 AM Page 88
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
5
Strings and Regular Expressions
Terms You’ll Need to Understand
n
The == and === operators
n
Regular expression
n
PCRE
Techniques You’ll Need to Master
n
Formatting strings
n
Comparing strings
n
Modifying string contents
n
Using regular expressions for pattern matching and extraction.
n
Joining and splitting strings
The Web is largely a text-oriented environment. Data is submitted to websites in the
form of text strings, and the response (be it in HTML, XML, or even an image format)
is generally text as well. Accordingly, being able to analyze and manipulate text is a core
skill of any PHP programmer.
Comparing Strings
In this section, you will learn how to test whether two strings are equal, or whether one
string exists inside of another string.
06 7090 ch05 7/16/04 8:42 AM Page 89
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
90
Chapter 5 Strings and Regular Expressions
Comparison with == and ===
The most basic way of comparing any two entities in PHP is using the == operator
(called the is equal operator).When the == operator tests the equivalence of two entities,
it first reduces them to a common type.This often causes unexpected results. For exam-
ple, the following code outputs $a and $b are equal:
$a = ‘Hello World’;
$b = 0;
if($a == $b) {
print “\$a and \$b are equal\n”;
} else {
print “\$a and \$b are not equal\n”;
}
The reason this happens is that $a is a string type and $b is an integer, so the Zend
Engine needs to convert them to a common type for comparison. == is a weak operator,
so it converts to the more lenient type, namely integer.The integer representation of
‘Hello World’ is 0, so $a == $b is true. == should only be used to compare strings if
you are certain that both its operands are in fact strings.
PHP also provides the stronger equivalence operator === (called the is identical opera-
tor).Whereas the == was too weak to be useful in many situations, === is often too
strong. === performs no type-homogenization, and requires that both operands be of the
same type before a comparison can be successful.Thus, the following code outputs $a
and $b are not equal:
$a = 1;
$b = “1”;
if($a === $b) {
print “\$a and \$b are equal\n”;
} else {
print “\$a and \$b are not equal\n”;
}
This result occurs because $a is internally held as an integer, whereas $b, by virtue of its
being quoted, is a string.
Thus, === can be dangerous to use if you are not certain that both operands are
strings.
Tip
You can force a variable to be cast to strings by the use of casts. Thus,
if( (string) $a === (string) $b) { }
will convert both $a and $b to strings before performing the conversion. This produces the results you
expect, but is a bit clumsy—using the strcmp family of functions is generally preferred.
06 7090 ch05 7/16/04 8:42 AM Page 90
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
91
Comparing Strings
Using strcmp and Friends
The preferred way of comparing two entities as strings is to use the strcmp() function.
strcmp() takes two arguments and compares them lexigraphically (also known as diction-
ary ordering, as it is the same logic used in sorting words in a dictionary). strcmp()
returns 0 if the two strings are identical.Thus this code, which gave us trouble before,
will correctly output that $a and $b are the same:
$a = 1;
$b = “1”;
if(strcmp($a, $b) == 0) {
print “\$a and \$b are the same\n”;
} else {
print “\$a and \$b are different\n”;
}
If its two operands are not the same, strcmp() will return -1 if the first operand would
appear before the second in a dictionary, and 1 if the first operand would appear after
the second in a dictionary.This behavior makes it very useful for sorting arrays of words.
In fact, the following two bits of code will sort the array $colors in the same fashion
(in dictionary order):
$colors = array(“red”, “blue”, “green”);
sort($colors, SORT_STRING);
and
$colors = array(“red”, “blue”, “green”);
usort($colors, ‘strcmp’);
By itself, this is not very useful. (sort() should be preferred over usort() when per-
forming equivalent tasks), but strcmp() has some sibling functions that perform similar
tasks.
strcasecmp() is identical to strcmp() except that it performs comparisons that are
not case sensitive.This means that the following code that will output $a is the same as
HELLO, modulo case:
$a = ‘hello’;
if(strcasecmp($a, ‘HELLO’)) {
print “\$a is the same as HELLO, modulo case\n”;
}
Also, RED will come after blue when sorted via strcasecmp(), whereas with strcmp(),
RED will come before blue.
06 7090 ch05 7/16/04 8:42 AM Page 91
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
92
Chapter 5 Strings and Regular Expressions
Matching Portions of Strings
You’ve seen how to match strings exactly, but sometimes you only need to match a por-
tion of a string.When only a portion of a string is considered, it is referred to as a sub-
string. Specifically, a substring is any portion of a string. For example, PHP is a substring of
the string PHP is a scripting language.
Matching Leading Substrings
To match only the leading portion of strings, PHP provides the strncmp() family of
functions. strncmp() and strncasecmp() are identical to strcmp() and strcasecmp(),
but both take a third parameter, $n, that instructs PHP to compare only the first $n char-
acters of both strings.Thus strncmp(‘figure1.gif’, ‘figure2.gif’, 6) will return
0 (equal) because only the first six characters of each string is compared.
Matching Substrings at Arbitrary Offsets
If you need to determined simply whether a substring exists anywhere inside a given
string, you should use strstr(). strstr() takes as its first argument a string to be
searched (often called the subject), and as its second the substring to search for (often
called the search pattern). If strstr() succeeds, it will return the searched for substring
and all text following it; otherwise, it returns false.
Here is a use of strstr() to determine whether the word PHP appears in the string
$string:
if(strstr($string, ‘PHP’) !== FALSE) {
// do something
}
If you want to search for a substring irrespective of case, you can use stristr(). Here is
a check to see if any forms of ‘PHP’ (including ‘php’, ‘Php’, and so on) appear in
$string:
if(stristr($string, ‘PHP’) !== FALSE) {
// do something
}
If instead of the actual string you would like the position of the match returned to
you, you can use strpos(). strpos() works similarly to strstr(), with two major
differences:
n
Instead of returning the substring containing the match, strpos() returns the
character offset of the start of the match.
n
strpos() accepts an optional third parameter that allows you to start looking at a
particular offset.
Here is a sample usage of strpos() to find every starting position of the substring ‘PHP’
in a search subject $string.
06 7090 ch05 7/16/04 8:42 AM Page 92
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
93
Formatting Strings
$offset = 0;
$match_pos = array();
while(($offset = strpos($string, ‘PHP’, $offset)) !== FALSE) {
$match_pos[] = $offset;
}
strpos() also has a not case-sensitive form, stripos(), that behaves in a similar fashion.
Tip
Because the first character in a string is at position 0, you should always use === to test whether a match
from strpos() succeeded or failed.
If you need to match from the end of your subject backwards, you can do so with
strchr(), strrpos(), or strripos(). strrpos() and strripos() behave identically to
strpos() and stripos() with the exception that they start at the end of the subject
string and that the search pattern can only be a single character. strrchr() behaves like
strstr(), returning the matched character and the rest of the subject following it, but it
also requires a single character search pattern and operates starting at the end of the sub-
ject (this is in contrast with the majority of strr* functions, which take full strings for
all their arguments).
Formatting Strings
Specifying specific formats for strings is largely a leftover from compiled languages such
as C, where string interpolation and static typing make it more difficult to take a collec-
tion of variables and assemble them into a string. For the most part, PHP will do all of
this for you. For example, most string formatting looks like this:
$name = ‘George’;
$age = 30;
print “$name is $age years old.”;
When variables are placed inside a double-quoted string, they are automatically expand-
ed. PHP knows how to convert numbers into strings as well, so $age is correctly
expanded as well.
Occasionally, however, you need to perform more complex formatting.This includes
the padding of numbers with 0s (for example, displaying
05 instead of 5), limiting the
printed precision of floating point numbers, forcing right-justification, or limiting the
number of characters printed in a particular string.
printf Formats
The basic function for formatting is printf(). printf() takes a format string and a list
of arguments. It then passes through the formatting string, substituting special tokens
contained therein with the correctly formatted arguments.
06 7090 ch05 7/16/04 8:42 AM Page 93
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
94
Chapter 5 Strings and Regular Expressions
Formatting tokens are denoted with a %. In their simplest form, this is followed
directly by a type specifier from Table 5.1.
Table 5.1 printf() Format Specifiers
Specifier Format
b The argument is treated as an integer and is presented as an integer in binary
form.
c The argument is treated as an integer and is presented as the ASCII character
for that value.
d The argument is treated as an integer and presented as a signed integer.
u The argument is treated as an integer and presented as an unsigned integer.
f The argument is treated as a floating-point number and presented as a float-
ing-point number.
o The argument is treated as an integer and presented as its octal representation.
x The argument is treated as an integer and presented as a hexadecimal number
(using lowercase letters).
X The argument is treated as an integer and presented as a hexadecimal number
(using uppercase letters).
Thus, the preceding simple code block that prints $name and $age can be rewritten as
follows:
printf(“%s is %d years old”, $name, $age);
By itself, this is not terribly useful.Though it might be slightly more readable than using
interpolated variables (especially to people coming from a C or Java background), it is
also slower and not more flexible.
The usefulness of the formatting functions comes via the format modifiers that can
be added between the % and the format specifier, from right to left:
n
A floating-point precision, given by a . followed by the desired precision that says
how many decimal places should be displayed for a floating point number. Note
that this will round numbers to the specified precision.
n
A field width that dictates how many characters minimum should be displayed for
this token. For example, to guarantee that at least eight characters are allotted for
an integer, you would use the format specifier “%8d”. By default, blank spaces are
used to pad the results.
n
To left-justify a formatting, a - can be added to the format.
n
Instead of using blank spaces, an expansion can be padded with 0s by preceding
the width-specifier with a 0.Thus, if you are printing time in 24-hour notation
(such that one o’clock is printed as 01:00), you can use the following:
printf(“%02d:%02d”, $hour, $minute);
06 7090 ch05 7/16/04 8:42 AM Page 94
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
95
Extracting Data from Strings
Optionally, a different character can be specified by escaping it with a ‘. So to pad all
your numbers with xxx, you would use
printf(“%’xd”, $number);
printf() Family Functions
PHP has a small collection of formatting functions that are differentiated from each
other by how they take their arguments and how they handle their results.
The basic function (which you saw previously) is printf(). printf() takes a format
string and a variable number of arguments that it uses to fill out the format string. It
outputs the result.
fprintf() is identical to printf(), except that instead of writing output to the stan-
dard display stream, it writes output to an arbitrary stream resource specified as the first
parameter.
sprintf() is identical to printf(), but instead of outputting its results, it returns
them as a string.
vprintf() takes its arguments as a single array (instead of a variable number of indi-
vidual arguments) and outputs the result.This is useful when you are passed a variable
number of arguments—for example, via call_user_func_array() or
func_get_args().
vsprintf() is identical to vprintf(), except that instead of outputting its result, it
returns it as a string.
Table 5.2 is a complete listing of all the formatting functions, with a list of the args
they take and where their result is sent (as output, to an arbitrary stream, or to a string).
Table 5.2 Formatting Functions
Function Args Result
printf format, args writes output
sprintf format, args returns result
vprintf format, array of args writes output
vsprintf format, array of args returns result
fprintf stream resource, writes output to
format, args stream
Extracting Data from Strings
When dealing with data that comes in from an external source (for example, read from a
file or submitted via a form), complex data is often packed into strings and needs to be
extracted. Common examples include decomposing phone numbers, credit card num-
bers, and email addresses into their base components. PHP provides both basic string
functions for efficiently extracting data in fixed formats, as well as regular expression
facilities for matching more complex data.
06 7090 ch05 7/16/04 8:42 AM Page 95
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
96
Chapter 5 Strings and Regular Expressions
Extracting Substrings by Offset
To extract a substring by offset, you can use the substr() function. substr() works by
taking a string (the subject), an offset from the beginning of the string from which to
start, and an optional length (by default, the remainder of the string from which the start
offset is grabbed).
For example, to get all of $string except for the first character, you can use the
following:
$result = substr($string, 1);
or to grab the first eight characters of a string, you can use this code:
$result = substr($string, 0, 8);
For a more nontrivial example, consider this code that grabs the local part of an email
address (the part before the @ character) by using strpos() to find the @ symbol and
substr() to extract the substring preceding it:
$local_part = substr($email, 0, strpos($email, ‘@’));
If you need to grab a substring at the end of your subject, you can use a negative offset
to indicate that your match is relative to the end of a string. For example, to grab the last
four characters of a string, you can do the following:
$result = substr($email, -4);
If you need to only access individual characters in a string, you can use curly braces ({})
to access the string’s characters by offsets. For example, to iterate over every character in
a string and capitalize the odd numbered characters, you can do the following:
$len = strlen($string);
for($i = 0; $i < $len; $i++) {
if($i % 2) {
$string{$i} = strtoupper($string{$i});
}
}
Extracting Formatted Data
Real-world data extraction tasks often involve strings that have vague formats. Complex
data extraction usually requires the use of regular expressions (covered later in this chap-
ter), but if the data is of a format that can be specified with a printf() formatting
string, you can use sscanf() to extract the data.
For example, to match IP address/port number pairings of the form
127.0.0.1:6137, you can use the format “%d.%d.%d.%d:%d”.That can be used with
sscanf() as follows:
$parts = sscanf($string, “%d.%d.%d.%d:%d”);
06 7090 ch05 7/16/04 8:42 AM Page 96
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
97
Modifying Strings
If $string is 127.0.0.1:6137, $parts will be filled out thusly:
Array
(
[0] => 127
[1] => 0
[2] => 0
[3] => 1
[4] => 6137
)
Though flexible, sscanf() parsing is a bit fragile:The pattern must match exactly (mod-
ulo whitespace) at the beginning of the subject string.
Modifying Strings
In this section, you will see how to modify strings by replacing substrings, both by the
offset of where you want to perform the replacement and by simple pattern match (for
example, replacing all occurrences of ‘foo’ with ‘bar’).
Modifying Substrings by Offset
To replace a substring in a subject string, you can use the substr_replace() function.
substr_replace()’s first argument is a subject string; its second a replacement string; its
third the offset to start the replacement at; and its optional fourth argument is the length
of the subject substring to replace.
To illustrate this, consider how to X out all but the last four digits of a credit card
number. Here is code to perform this with substr_replace():
$len = strlen($ccnum);
$newnum = substr_replace($ccnum, str_repeat(‘X’, $len -4), 0, $len - 4);
First, you find the length of the credit card number in question, and then you replace the
first $len - 4 characters with an equal number of X’s.
Replacing Substrings
Another common string modification task is replacing all occurrences of one substring
with another.The preferred function for doing this is str_replace(). str_replace()
takes as its first argument a string to be matched, and as its second the string to substitute
in. Its third parameter is the subject on which all this replacement should occur. For
example, to replace all occurrences of :) with the image link <img
, you can use the following replacement:
$new_subject = str_replace(‘:)’, ‘<img src=”/smiley.png”/>’, $subject);
06 7090 ch05 7/16/04 8:42 AM Page 97
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

Không có nhận xét nào:

Đăng nhận xét