Correcting teacher:天蓬老师
Correction status:Uncorrected
Teacher's comments:
$roleIds = [1, 3, 5, 7, 9];
$roleIdStr = implode(',', $roleIds);
echobr($roleIdStr);
/*
result: 1,3,5,7,9
*/
$data = '2&4&6&8&10';
$arr = explode('&', $data);
printfpre($arr);
/*
result:
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
)
*/
$str = "Mom said:'Life is like a box of chocolate.'";
// test1:截取妈妈说的话的第一个单词
$firstWord = substr($str, 10, 4);
echobr($firstWord);
/*
result:Life
*/
/* 第二个参数也可以是负数,代表从字符串最有一个字符开始截取 */
// test2:截取妈妈说的话的最后一个单词
$lastWord = substr($str, -11, 9);
echobr($lastWord);
/*
result: chocolate
*/
/* 第三个参数也可以是负数, 此时它是代表截取的字符串最后的位置是从字符串的最后一个字符开始数的第(-n)个字符 */
// test2:截取出妈妈说话的内容
$monSaid = substr($str, 10, -2);
echobr($monSaid);
/*
result: Life is like a box of chocolate
*/
// test1:查找ing字符串出现的次数
$str = 'goingsettingfinishingstartinglovinghating';
$target = 'ing';
$count = substr_count($str, $target);
echobr('共出现' . $count . '次');
/*
result: 共出现6次
*/
// test2:从第11个字符开始查找
$count = substr_count($str, $target, 11);
echobr('共出现' . $count . '次');
/*
result: 共出现4次
*/
// test3: 从倒数第20个字符开始查找,查找15个字符
$count = substr_count($str, $target, -20, 15);
echobr('共出现' . $count . '次');
/*
分别是starting和loving中的ing
result: 共出现2次
*/
// test4: 从倒数第20个字符开始查找, 到倒数第3个字符为止
echobr('共出现' . substr_count($str, $target, -20, -2) . '次');
/*
result: 共出现2次
*/
// test1:把玩手机游戏替换为学习
$str = 'I like playing phone games';
echobr(substr_replace($str, 'study', 7));
/*
result: I like study
*/
// test2:再加上x-box游戏
echobr(substr_replace($str, ' and x-box games', strlen($str)));
/*
result: I like playing phone games and x-box games
*/
// test3:把手机游戏换成x-box游戏
echobr(substr_replace($str, 'x-box', 15, -6));
/*
result: I like playing x-box games
*/
// test4:另一种写法
echobr(substr_replace($str, 'x-box', -11, -6));
/*
result: I like playing x-box games
*/
$str = 'abcdefghijklmnopqrst uvwxyz';
// test1:分割成单个字符
dumpbr(str_split($str));
/*
result: array(27) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" [4]=> string(1) "e" [5]=> string(1) "f" [6]=> string(1) "g" [7]=> string(1) "h" [8]=> string(1) "i" [9]=> string(1) "j" [10]=> string(1) "k" [11]=> string(1) "l" [12]=> string(1) "m" [13]=> string(1) "n" [14]=> string(1) "o" [15]=> string(1) "p" [16]=> string(1) "q" [17]=> string(1) "r" [18]=> string(1) "s" [19]=> string(1) "t" [20]=> string(1) " " [21]=> string(1) "u" [22]=> string(1) "v" [23]=> string(1) "w" [24]=> string(1) "x" [25]=> string(1) "y" [26]=> string(1) "z" }
*/
// test2:按定长分割
dumpbr(str_split($str, 7));
/*
result: array(4) { [0]=> string(7) "abcdefg" [1]=> string(7) "hijklmn" [2]=> string(7) "opqrst " [3]=> string(6) "uvwxyz" }
*/
// 1. 所有可选项均不入参,则默认使用空格" "填充到制定长度
echobr('=>' . str_pad('hello, GuangXi NanNing!', 130) . '<=');
/*
result: =>hello, GuangXi NanNing! <=
只填充了一个空格?因为浏览器解析多个连续空格时只解析一个
*/
// 2. 可选项只入参填充长度,填充方向不入参(默认向右填充)
echobr(str_pad('hello, GuangXi NanNing!', 30, '!'));
/*
result: hello, GuangXi NanNing!!!!!!!!
*/
// 3. 指定填充方向(STR_PAD_LEFT:向左填充;STR_PAD_RIGHT:向右填充(默认);STR_PAD_BOTH:两端填充)
echobr(str_pad('hello, GuangXi NanNing!', 30, '!', STR_PAD_LEFT));
/*
result: !!!!!!!hello, GuangXi NanNing!
*/
echobr(str_repeat('重要的事情说三遍!', 3));
/*
result: 重要的事情说三遍!重要的事情说三遍!重要的事情说三遍!
*/
// 1. 查找一个子串,替换成一个子串,处理一个字符串
echobr(str_replace('ing', '<=>', 'doingsleepingwritingsettingcuttingdone'));
/*
result: do<=>sleep<=>writ<=>sett<=>cutt<=>done
*/
// 2. 查找多个子串,替换成一个子串,处理一个字符串
echobr(str_replace(['m', 'n'], 'o', 'I am from GuangXi NanNing, China.'));
/*
result: I ao froo GuaogXi NaoNiog, Chioa.
*/
// 3. 查找多个子串,替换成多个子串,处理一个字符串
echobr(str_replace(['m', 'n'], ['o', 'p'], 'I am from GuangXi NanNing, China.'));
/*
result: I ao froo GuapgXi NapNipg, Chipa.
*/
/* 删除 */
echobr(str_replace(['m', 'n'], '', 'I am from GuangXi NanNing, China.'));
/*
result: I a fro GuagXi NaNig, Chia.
*/
// 4. 查找多个子串,替换成多个子串,处理多个字符串
printfpre(str_replace(['m', 'n'], ['o', 'p'], ['I am from GuangXi NanNing, China.', 'commingmoonmultiplymanyname']));
/*
result:
Array
(
[0] => I ao froo GuapgXi NapNipg, Chipa.
[1] => coooipgooopoultiplyoapypaoe
)
*/
// 5. 记录替换次数
str_replace('n', 'o', 'I am from GuangXi NanNing, China.', $count);
echobr("共替换了{$count}个'n'");
/*
result: 共替换了4个'n'
*/
/* i=>e; n=>d; g=; */
echobr(strtr('doing coming setting going', 'ing', 'ed;'));
/*
result: doed; comed; setted; ;oed;
*/
// 2. strtr(待处理字符串,替换对数组):当有多个替换对时,以数组的形式传入
echobr(strtr('doing coming setting going', ['i' => 'e', 'n' => 'd', 'g' => ';']));
/*
result: doed; comed; setted; ;oed;
*/
echobr(str_shuffle('abcdefghijklmnopqrstuvwxyz'));
/*
result: wxzvqgptacjdhbfkyeslnoiurm
*/
// test1:列宽设置为17,超出的字符串打断后做换行(<br/>)处理
echobr(wordwrap("i am the 1st row. i am the 2nd row. i am the 3rd row.", 17, '<br/>'));
/*
result:
i am the 1st row.
i am the 2nd row.
i am the 3rd row.
*/
// test2:列宽还是17,超出的字符串打断后改成分列(</td><td>)处理
$content = wordwrap("i am the 1st row. i am the 2nd row. i am the 3rd row.", 17, '</td><td>');
$tbl = <<< EOP
<table border="1" cellspacing="0">
<tr>
<td>
{$content}
</td>
</tr>
</table>
EOP;
echobr($tbl);
/*
result:
<table cellspacing="0" border="1">
<tbody><tr>
<td>
i am the 1st row.</td><td>i am the 2nd row.</td><td>i am the 3rd row.
</td>
</tr>
</tbody></table>
*/
// 字符串长度超过列宽,可以默认不打断,可以设置最后一个参数将其打断
echobr(wordwrap('congratulations!', 10, '<br/>', true));
/*
resulta:
congratula
tions!
*/
// test1:去空格
$str = ' hello everyone! ';
echobr(chop($str));
/*
result: hello everyone!
*/
// test2:去掉其他字符(多个字符以字符串的形式传入,忽略顺序,首尾处只要出现字符串中的字符,都会被去除)
$str = 'hello everyone';
echobr(trim($str, "ehlo"));
/*
result: everyon
*/
$target = "ing";
$str = 'going setting going getting starting';
/* test1: */
echobr('"ing"首次出现的位置是:' . (strpos($str, $target) + 1));
/*
result: "ing"首次出现的位置是:3
*/
/* test2:从第4个字符开始搜索 */
echobr('"ing"首次出现的位置是:' . (strpos($str, $target, 3) + 1));
/*
result: "ing"首次出现的位置是:11
*/
/* test3:*/
echobr('"ing"最后一次出现的位置是:' . (strrpos($str, $target) + 1));
/*
result: "ing"最后一次出现的位置是:34
*/
/* test4: 忽略大小写 */
$str = 'goIng settiNg goinG getting startING';
echobr('"ing"忽略大小写时首次出现的位置是:' . (1 + stripos($str, $target)));
echobr('"ing"不忽略大小写时首次出现的位置是:' . (1 + strpos($str, $target)));
echobr('"ing"忽略大小写时最后一次出现的位置是:' . (1 + strripos($str, $target)));
/*
result:
"ing"忽略大小写时首次出现的位置是:3
"ing"不忽略大小写时首次出现的位置是:25
"ing"忽略大小写时最后一次出现的位置是:34
*/
$url = 'www.php.cn/0422/homework/homework.php?username=zhangsan&password=123456';
/* test1:向后截取 */
echobr('url的参数部分:' . strstr($url, '?'));
/*
result: url的参数部分:?username=zhangsan&password=123456
*/
/* test2:向前截取 */
$url = strstr($url, '?', true);
echobr('url的访问路径:' . strstr($url, '/'));
/*
result: url的访问路径:/0422/homework/homework.php
*/
$str = 'goIng settiNg goinG getting starting';
/* test3:忽略大小写 */
echobr(stristr($str, 'ing'));
/*
result: Ing settiNg goinG getting startING
*/
/* test4:从最有一次找到的位置开始截取 */
echobr(strrchr($str, 'ing'));
/*
result: ing
*/
$str = 'goIng settiNg goinG getting starting';
$targets = 'esc';
echobr(strpbrk($str, $targets));
/*
第一个匹配成功的字符是: s
result: settiNg goinG getting starting
*/
$str = 'getting goinG getting starting';
$targets = 'teing';
/* test1: */
// 默认从字符串开始搜索到未尾
echobr(strspn($str, $targets));
/* 匹配到的子串是getting
result: 7
*/
/* test2:指定匹配开始位置(可以是负数索引) */
echobr(strspn($str, $targets, -4));
/*
匹配到的子串是最后starting中的ting
result: 4
*/
/* test3:指定查询的字符串长度(第四个参数为非负数时) */
echobr(strspn($str, $targets, 0, 3));
/*
匹配到的子串是get
result: 3
*/
/* test4:指定查询的结束位置(第四个参数为负数时) */
echobr(strspn($str, $targets, 0, -10));
/*
实际的待处理的字符串是: getting goinG gettin;最后的g starting这10个字符不在处理范围
匹配到的是一开始的getting.
result: 7
*/
$str = 'ABCD EFG HIJK LMN opq rst uvw xyz';
/* test1:全部转小写 */
$res = strtolower($str);
echobr($res);
/*
result: abcd efg hijk lmn opq rst uvw xyz
*/
/* test2:全部转大写 */
echobr(strtoupper($str));
/*
result: ABCD EFG HIJK LMN OPQ RST UVW XYZ
*/
/* test3:首字母小写 */
echobr(lcfirst($str));
/*
result: aBCD EFG HIJK LMN opq rst uvw xyz
*/
/* test4:首字母大写 */
echobr(ucfirst($res));
/*
result: Abcd efg hijk lmn opq rst uvw xyz
*/
/* test5:单词首字母大写 */
echobr(ucwords($res));
/*
result: Abcd Efg Hijk Lmn Opq Rst Uvw Xyz
*/
// test1: 英文+特殊字符等
$str = 'abcdefghijklmnopqrstuvwxyz +_*&^%$#@!~-=`';
echobr(strlen($str));
/*
result: 41
*/
// test2:中文
$str = '我爱PHP';
echobr(strlen($str));
/*
utf-8编码下的一个中文占3个字节
result: 9
*/
// test1:有中文,输出乱码,证明是按字节反转
$str = '猪是来过反';
echobr(strrev($str));
// test2:
$str = 'hello boys and girls';
echobr(strrev($str));
/*
result: slrig dna syob olleh
*/
$str = <<< EOP
<h1>这里是h1标题</h1>
<?php echo "<span>php生成的html代码</span>"; ?>
hello everyone!
EOP;
echobr(strip_tags($str));
/*
result: 这里是h1标题 hello everyone!
*/
$param = 'user=admin&password=123456&sex=male&realname=zhang+san&data[]=1&data[]=2';
/* 不需要问号了 */
// test1:直接解析为当前作用域的变量(不推荐此用法)
parse_str($param);
/* 解析完成后可以在当前作用域直接使用这些变量 */
echobr($user);
echobr($password);
echobr($sex);
echobr($realname);
dumpbr($data);
/*
result:
123456
male
zhang san
array(2) { [0]=> string(1) "1" [1]=> string(1) "2" }
*/
// test2:解析结果作为数组元素存到第二个参数中
parse_str($param, $dt);
printfpre($dt);
/*
result:
Array
(
[user] => admin
[password] => 123456
[sex] => male
[realname] => zhang san
[data] => Array
(
[0] => 1
[1] => 2
)
)
*/
$url = 'http://www.php.edu/0422/homework/homework.php?user=admin&password=123456#abcdefg';
// test1:返回解析的所有结果组成的数组
printfpre(parse_url($url));
/*
result:
Array
(
[scheme] => http
[host] => www.php.edu
[path] => /0422/homework/homework.php
[query] => user=admin&password=123456
[fragment] => abcdefg
)
*/
// test2:只返回某个部分(示例中返回查询参数)
/* 能返回的部分: PHP_URL_SCHEME、 PHP_URL_HOST、 PHP_URL_PORT、 PHP_URL_USER、 PHP_URL_PASS、
PHP_URL_PATH、 PHP_URL_QUERY 或 PHP_URL_FRAGMENT */
printfpre(parse_url($url, PHP_URL_QUERY));
/*
result: user=admin&password=123456
*/
$data = [
'name' => 'admin', 'realname' => 'zhang san', 'password' => '123456', 'roleId' => [1, 2, 3]
];
// test1:
echobr(http_build_query($data));
/*
result: name=admin&realname=zhang+san&password=123456&roleId%5B0%5D=1&roleId%5B1%5D=2&roleId%5B2%5D=3
*/
// test2:指定字段前缀,键值对连接符指定为;
echobr(http_build_query($data, 'db_', ';'));
/*
result: name=admin;realname=zhang+san;password=123456;roleId%5B0%5D=1;roleId%5B1%5D=2;roleId%5B2%5D=3
*/
$param = 'http://www.php.edu/0422/homework/homework.php?user=admin&password=123456&sex=male&realname=zhang+san&data[]=1&data[]=2';
// test1:编码
$res = urlencode($param);
echobr($res);
/*
result: http%3A%2F%2Fwww.php.edu%2F0422%2Fhomework%2Fhomework.php%3Fuser%3Dadmin%26password%3D123456%26sex%3Dmale%26realname%3Dzhang%2Bsan%26data%5B%5D%3D1%26data%5B%5D%3D2
*/
// test2:解码
echobr(urldecode($res));
/*
result: http://www.php.edu/0422/homework/homework.php?user=admin&password=123456&sex=male&realname=zhang+san&data[]=1&data[]=2
*/
// test1: 编码
$img = base64_encode(file_get_contents('../pic.png'));
// echobr($img);
echo "<img src='data:image/>jpeg;base64,{$img}'>";
// test2: 解码, 把编码后的图片还原, 并保存为文件
file_put_contents('girl.jpg', base64_decode($img));
$html = <<< EOP
<h1>我是'h1'标题</h1>
<span><<<姓 名>>></span>
EOP;
// test1
/* 转义后所有特殊字符会原样输出 */
echobr(htmlspecialchars($html));
/* 不经过转义,特殊字符将会被浏览器解析 */
echobr($html);
/*
result:
<h1>我是'h1'标题</h1>
<span><<<姓 名>>></span>
<h1>我是'h1'标题</h1>
<span><<<姓 名>>></span>
*/
$html1 = <<< EOP
<h1>我是'h1'标题</h1>
<span><<<姓 名>>></span>
EOP;
// test1
/* 转换后所有字符会被浏览器解析 */
echobr(htmlspecialchars_decode($html1));
/* 不转换, 特殊字符会原样输出 */
echobr($html1);
/*
result:
<h1>我是h1标题</h1>
<span><<<姓 名>>></span>
<h1>我是h1标题</h1>
<span><<<姓 名>>></span>
*/
// test1:单引号和双引号也转义
$html2 = <<< EOP
<h1>我是'h1'标题</h1>
<span><<<姓 名>>></span>
EOP;
echobr(htmlentities($html2, ENT_QUOTES));
/*
result:
<h1>我是'h1'标题</h1>
<span><<<姓 名>>></span>
*/
// test1: 单引号和双引号也转换
$html3 = <<< EOP
<h1>我是'h1'标题</h1>
<span><<<姓 名>>></span>
EOP;
echobr(html_entity_decode($html3, ENT_QUOTES));
/*
result:
<h1>我是'h1'标题</h1>
<span><<<姓 名>>></span><br>
*/
<br/>
$html4 = "离离原上草\n一岁一枯荣\r\n野火烧不尽\n春风吹又生";
echobr(nl2br($html4));
/*
离离原上草<br />
一岁一枯荣<br />
野火烧不尽<br />
春风吹又生<br>
*/
暂时不感兴趣, 略过…
$str = 'hello everyone!';
$salt = 'anbmcldkejfigh1234567890';
$str .= $salt;
echobr(md5($str));
/*
result: f86aa82a662a37bb650c849fc6df7363
*/
$str = 'hello everyone!';
$salt = 'anbmcldkejfigh1234567890';
$str .= $salt;
echobr(sha1($str));
/*
result: f19831b4b4530f535a1688a5dc2cbe03673bdbda
*/