作业:
实例
<?php $str = '希望与会各方加强对话,扩大合作,实现互利共赢'; //截取'扩大合作' $res = mb_substr($str,11,4); echo $res,"<br>"; //查找“互利”单词,返回的是带查询的之后的字符串,若第三个参数true则返回查询字段左边的字符串 //希望与会各方加强对话, $res = mb_strstr($str, "扩大",true); echo $res,"<br>"; $str = "my name is tim,i am 15 year old."; //strpos 返回查询内容所在的位置 返回int 23 $res = strpos($str, 'year'); echo $res."<br>"; $str = "my name is tim,tim is 15 year old."; //替换操作 my name is lucy,lucy is 15 year old. $res = str_replace("tim", "lucy", $str); echo $res."<br>"; //使用数组替换多个 my name is jack,jack is 25 year old. $res = str_replace(['tim','15'], ['jack','25'], $str); echo $res."<br>"; //指定替换字符串 my name is tom,he is 15 year old. $res = substr_replace($str,'tom,he',11,7); echo $res."<br>"; //创建一个a-z的有序数组,开始排序。 $person = ['tom','jack','lucy','tim']; $arr = array(); foreach ($person as $k => $v) { $arr[$v] = array_rand(array_flip(range('a','z')),10); shuffle($arr[$v]); } $arr['小明'] = [1,5,6,2,4,8,9,4,44,5,55,89,56,1,66666,9956,956,922]; $arr['小芳'] = [1,5,6,2,"$#",4,8,'*',4,"++",5,52,'--%^@','15&*(']; // var_dump($arr); foreach ($arr as $k=>$v) { $v = fn($v); $arr[$k] = $v; } //一维数组对比函数 function fn($arr){ usort($arr,function($a,$b){ //如果传入数组的元素类型进行比较 $type1 = is_numeric($a) && is_numeric($b);//数字与数字 $type2 = is_numeric($a) && is_string($b);//数字与字符串 $type3 = is_string($a) && is_numeric($b);//字符串与数字 if($type1){ if ($a==$b) return 0; return ($a<$b)?-1:1; }else if ($type2) { return 1; }else if ($type3) { return -1; }else{ return strcmp($a,$b); } }); return $arr; } var_dump($arr); ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例