ZendChina | Zend中文权威资讯's Archiver

ZendChina 发表于 2008-1-31 16:52

Zend认证试题第五章(Strings and Regular Expressions)

        ZendChina官方:字符串是PHP的“瑞士军刀”——作为一种Web开发语言,PHP最常打交道的就是字符串。因此对于开发者来说,处理字符串是一项非常基础的技能。
幸运的是,由于PHP开发团队的努力,PHP对字符串的处理相当易学。你只需迈过第一个难关,接下来就一马平川了。
       但是,PHP的这一部分功能并非完美。本章考验你对字符串的了理解及对处理字符串的函数的认识。此外,你还必须面对正则表达式——一个非常有用,却总是被开发者忽试的工具——的编写艺术。

1. Consider the following script. What line of code should be inserted in the marked location in order to display the string php when this script is executed?
[php]<?php
$alpha = 'abcdefghijklmnopqrstuvwxyz';
$letters = array(15, 7, 15);
foreach($letters as $val) {
/* What should be here */
}
?>[/php]
A. echo chr($val);
B. echo asc($val);
C. echo substr($alpha, $val, 2);
D. echo $alpha{$val};
E. echo $alpha{$val+1}

2. Which of the following will not combine strings $s1 and $s2 into a single string?
A. $s1 + $s2
B. "{$s1}{$s2}"
C. $s1.$s2
D. implode('', array($s1,$s2))
E. All of the above combine the strings

3. Given a variable $email containing the string [email=user@example.com]user@example.com[/email], which of the following statements would extract the string example.com?

A. substr($email, strpos($email, "@"));
B. strstr($email, "@");
C. strchr($email, "@");
D. substr($email, strpos($email, "@")+1);
E. strrpos($email, "@");

4. Given a comma-separated list of values in a string, which function from the given list can create an array of each individual value with a single call?
A. strstr()
B. Cannot be done with a single function
C. extract()
D. explode()
E. strtok()

5. What is the best all-purpose way of comparing two strings?
A. Using the strpos function
B. Using the == operator
C. Using strcasecmp()
D. Using strcmp()

6. Which of the following PCRE regular expressions best matches the string php|architect?
A. .*
B. ...|.........
C. \d{3}\|\d{8}
D. [az]{3}\|[az]{9}
E. [a-z][a-z][a-z]\|\w{9}

7. Which of the following functions can be used to determine the integrity of a string? (Choose 3)
A. md5()
B. sha1()
C. str_rot13()
D. crypt()
E. crc32()

8. Which PHP function does the following script simulate on a UNIX machine?
[php]<?php
function my_funct ($filename)
{
$f = file_get_contents ($filename);
return explode ("\n", $f);
}
?>[/php]
A. fopen()
B. fread()
C. flock()
D. split_string()
E. file()

9. Which of the following functions can be used to break a string into an array based on a specific pattern? (Choose 2)
A. preg_split()
B. ereg()
C. str_split()
D. explode()
E. chop()

10. What will the following script output?
[php]<?php
echo 'Testing ' . 1 + 2 . '45';
?>[/php]
A. Testing 1245
B. Testing 345
C. Testing 1+245
D. 245
E. Nothing

11. What will be the output of the following script?
[php]<?php
$s = '12345';
$s[$s[1]] = '2';
echo $s;
?>[/php]
A. 12345
B. 12245
C. 22345
D. 11345
E. Array

12. Which of the strings below will be matched by the following PCRE regular expression? (Choose 2)
[php]/.*\*123\d/[/php]
A. ******123
B. *****_1234
C. ******1234
D. _*1234
E. _*123

13. Which of the following comparisons will return True? (Choose 2)
A. '1top' == '1'
B. 'top' == 0
C. 'top' === 0
D. 'a' == a
E. 123 == '123'

14. What happens if you add a string to an integer using the + operator?
A. The interpreter outputs a type mismatch error
B. The string is converted to a number and added to the integer
C. The string is discarded and the integer is preserved
D. The integer and string are concatenated together in a new string
E. The integer is discarded and the string is preserved

15. Consider the following script. Assuming that [url=http://www.php.net/]http://www.php.net[/url] can be successfully read, what will it output?
[php]<?php
$s = file_get_contents ("http://www.php.net");
strip_tags ($s, array ('p'));
echo count ($s);
?>[/php]
A. The length of the [url=http://www.php.net/]www.php.net[/url] homepage
B. The length of the [url=http://www.php.net/]www.php.net[/url] homepage stripped of all its <p> tags
C. 1
D. 0
E. The length of the [url=http://www.php.net/]www.php.net[/url] homepage stripped of all its tags except for <p> tags

16. The ___________ function can be used to compare two strings using a case-insensitive binary algorithm
A. strcmp()
B. stricmp()
C. strcasecmp()
D. stristr()
E. None of the above

17. Which of the following functions can be used to convert the binary data stored in a string into its hexadecimal representation? (Choose 2)
A. encode_hex()
B. pack()
C. hex2bin()
D. bin2hex()
E. printf()

18. The ________________ function can be used to ensure that a string always reaches a specific minimum length.
Your Answer: ____________________________

19. What will the following script output?
[php]<?php
$a = 'able osts indy';
echo wordwrap ($a, 1, "c", false);
?>[/php]
Your Answer: ____________________________

20. What will the following script output?
[php]<?php
$x = 'apple';
echo substr_replace ($x, 'x', 1, 2);
?>[/php]
A. x
B. axle
C. axxle
D. applex
E. xapple

ZendChina 发表于 2008-3-14 14:17

答案详解:
1.substr函数能够胜任,但考虑到输出三个字母就需要三次调用该函数,所以排除此方法。那么$alpha{$val}和$alpha{$val+1}是仅有的两个可能输出题目要求的字符串的选项。因为0是数组的第一个索引,所以答案是D。

2.除了A以外的选项都能输出题目要求的字符串。PHP中,加号(+)不能把两个字符串合并成一个。

3.substr函数返回字符串的一部分,而strpos函数擅长从一个字符串中找出某个指定的子串。同时使用这两个函数将满足题目要求。注意,前一个函数从0开始索引,而后者不是,因此需要+1。答案是D。

4.答案是D。explode函数使用一个字符串分隔另一个字符串,并把结果放入一个新建的数组。strtok函数也可以做同样的事,但需要多次调用。

5.答案是D。strcmp()提供了安全的字符串比较机制。注意,选项C是错的,strcasecmp()不是一个“万能”函数,因为它不区分大小写。

6.选项中没有一个正则表达式能真正代表题目所给字符串的匹配方式,但是选项A和E仍然能勉强匹配。选项A太普通了,它能够匹配任何字符串,因此答案是E。

7.正确答案是A,B和E。用crypt()和str_rot13()来验证一个字符串是否被改变,效率很低。crc32()比前面两个函数好些,如果能容忍一些小错误的话,它是个不错的选择。

8.file函数将文件的文本内容读入一个数组,每个元素是一行。因此答案E正确。也许你想知道为什么要把这样一个题目放在讲字符串的章节中,那是为了提醒你每一章的题目所包含的知识点并不是绝对严格区分开的,正如写PHP脚本时,file函数不能脱离字符串函数单独存在一样。

9.尽管条件不同,但preg_split和explode函数都能满足题目要求。ereg()拿一个正则表达式匹配一个字符串;str_split()按固定长度分隔字符串;而chop()则是rtrim()别名,用来移除字符串末尾处的空格。

10.本题考察你对字符串操作及操作符优先级的认识。连接运算符(.)的优先级比加号(+)高。因此PHP解释器实际执行的运算可以表示为('Testing ' . 1) + (2 . '45')。由于字符串test 1不是数字,所以加号前面的运算等于0。加号后面的运算等于245,PHP输出的结果是0+245,等于245,所以答案是D。

11.可以用访问数组元素的方式访问字符串中的字符,因此脚本只是把字符串中的第二个字符($s[1])替换成了字符2,最终将输出12245。答案是B。

12.本题的要点是理解这个正则表达式的含义——从左往右,首先是零个或多个任意字符(.*),跟着是一个星号(\*),然后是123,最后是一个数字。因此答案是C和D。

13.B和E正确。选项B中,在比较时,字符串top等同于数字0。==操作符不比对数据类型,所以将返回true。答案E中,字符串123等同于数字123,比较将返回true。

14.字符串将被转换成数字(如果无法发生转换就是0),然后与整型数字相加。答案是B。

15.代码的本意是剔除[url=http://www.php.net/][color=#0000ff]www.php.net[/color][/url]主页上除了p以外的的所有HTML标签。可实际上,在代码的最后一行使用了count函数,它统计变量中的元素数量,而不是字符串中的字符数。由于字符串是标量,对字符串使用count函数将永远返回1。答案是C。

16.题目其实就是在描述strcasecmp函数的作用,因此答案是C。

17.正确答案是B和D。pack函数能对二进制数据进行复杂的格式化,包括将字符串中的字符转化成十六进制表示。bin2hex函数也有同样的转化功能。注意,printf()能将整数转化成十六进制数,但无法转化字符串。

18.这是在说str_pad函数,它可以把字符串填充到指定长度。

19.脚本将输出ablecostscindy。wordwrap函数通常用来把字符串切割成指定长度。然而在本题中,长度被设置为1,因此函数将从空格处切割(第四个参数被设置为false,因此函数不会从单词的中间进行切割)。填充字符串是c,等于把每个空格都换成了c。

20.substr_replace函数是用一个指定字符串替换原字符串中的某个部分,因此脚本输出axle,答案是B。

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.