Correction status:qualified
Teacher's comments:
用usort()对二维数组排序
<?php /** * usort()二维数组的排序 */ $city = [ ['city' => '东莞', 'province' => '广东'], ['city' => '合肥', 'province' => '安徽'], ['city' => '南京', 'province' => '江苏'], ['city' => '泰安', 'province' => '山东'], ]; usort($city, function ($arr1, $arr2) { return strcmp($arr1['city'],$arr2['city']); }); echo '<pre>'; echo var_export($city,true);
点击 "运行实例" 按钮查看在线实例
常用字符串查找函数:substr(),strstr(),strpos()
<?php /** * substr(),strstr(),strpos()函数 */ //substr($str,$offset,[$length]) 根据位置查询子字符串 $str = 'I want to learn PHP well.'; echo substr($str,16).'<br>'; //输出:PHP well. echo substr($str,16,-3).'<br>'; //输出:PHP echo substr($str,-5).'<br>'; //输出:well. echo substr($str,-5,1).'<br>'; //输出:w echo substr($str,-5,-1).'<hr>'; //输出:well //strstr($str,$str2,bool) 根据某一内容来查询,参数二为 $str = 'I want to learn PHP well'; echo strstr($str,'l').'<br>'; //输出:learn PHP well echo strstr($str,'l',true).'<hr>'; //输出:I want to //strpos($str,$str2,$offset) 查找字符串首次出现的位置 $str = 'I want to learn PHP well'; echo strpos($str,'l').'<br>'; //返回10
点击 "运行实例" 按钮查看在线实例
常用字符串替换函数:str_replace()、substr_replace()
<?php /** * 字符串替换函数 * str_replace() substr_replace() */ /* str_replace($search,$replace,$str) */ //一般替换 $str = 'It\'s a wonderful day'; echo str_replace('wonderful','busy',$str).'<br>'; //一个替换多个 $str = 'I like the PHP,I\'m going to learn PHP'; echo str_replace(['like','going to'],'love',$str).'<br>'; //多个替换多个,一一对应 echo str_replace(['I','PHP'],['You','javacript'],$str).'<br>'; //删除字符串 $str = 'It\'s a wonderful day'; echo str_replace('wonderful','',$str); echo '<hr>'; /* substr_replace($str,$place,$start,[$length]) */ //全部替换 $str = 'I study PHP on PHP Chinese website'; echo substr_replace($str,'I\'m going to learn PHP',0).'<br>'; //部分替换 echo substr_replace($str,'javascript',8,3).'<br>'; //不指明长度则后面内容删除 //删除内容 echo substr_replace($str,'',12).'<br>'; echo substr_replace($str,'',15,3).'<br>'; //插入内容 echo substr_replace($str,'hefei ',15,0);
点击 "运行实例" 按钮查看在线实例