实例
<meta charset="utf-8"> <?php echo '作业一:实例演示substr(),strstr(),strpos()函数'; echo '<hr>'; echo 'substr(字符串,起始位置,长度)根据位置,精确查询','<br>'; $str = 'Do one thing at a time, and do well'; echo '从第8个开始输出:',substr($str,8),'<br>'; echo '从第8个开输出,取6个:',substr($str,8,3),'<br>'; echo '从倒数第3个输出:',substr($str,-3),'<hr>'; $company = 'php中文网'; echo strstr($company,'中'),'<br>'; echo strstr($company,'中',true),'<hr>'; echo 'strpos($str1,$str2)根据$str2内容查询返回字符串首次出现的位置','<br>'; echo strpos($str,'time'),'<hr>'; echo '作业二:字符串查找并替换的两个函数','<br>'; echo 'str_replace(search内容,replace内容,$str)查找并替换(单个内容或多个内容,或删除式替换)','<hr>'; echo '原字符串:',$str1 = 'Do one thing at a time, and do well','<br>'; echo '替换后:',str_replace('time','aaa',$str1),'<br>'; echo '替换后:',str_replace(['one','thing','and'],['aaa','bbb','ccc'],$str1),'<hr>'; echo 'substr_replace($str,search内容,起始位置/结束位置)精确位置查找并替换'; echo '原字符串:',$str2 = 'My monthly salary is 1000','<br>'; echo '替换后:',substr_replace($str2,' best friend is snooker',2); echo '<br>替换后:',substr_replace($str2,' 1000yuan',-4); echo '<hr>','作业三:usort()给二维数组子数组的值进行自定义排序'; $company = [['name'=>'aaa','products'=>'bbb','phone'=>'123456'],['name'=>'111','products'=>'222','phone'=>'333'],['name'=>'admin','products'=>'666666','phone'=>'88888888']]; echo '<pre>原数组',var_export($company,true),'<br><br>'; usort($company,function ($a,$b) { return strcmp($a['name'],$b['name']); }); echo '<pre>将多维子数组第二个元素排序后',var_export($company,true),'<br><br>';
运行实例 »
点击 "运行实例" 按钮查看在线实例