作业代码:
<?php echo "<h3>编程1: 实例演示substr(),strstr(),strpos()函数</h3><hr />"; $str="My name is staroom"; echo "<h4>substr()</h4>"; echo "<p>My name is staroom <br />取前两位:",substr($str,0,2),",取后7位:",substr($str,-7),",从第3开始,取4位:",substr($str,3,4),"</p>"; echo "<h4>strstr()</h4>"; echo "<p> 查询name是否存在,返回name及后面内容:",strstr($str,"name"),",返回前面内容:",strstr($str,"name",true)," </p>"; echo "<h4>strpos()</h4>"; echo "<p> 查询star首次出现的位置:",strpos($str,"star")," </p><hr />"; echo "<h3>编程2: 实例演示str_replace(), substr_replace()</h3><hr />"; $strs="My name is staroom. Staroom is a chinese"; echo "<h4>str_replace() str_ireplace()</h4>"; echo $strs,"<br />"; echo "<p> 替换staroom:",str_replace("staroom","Yuxicheng",$strs),"<br />忽略大小写:",str_ireplace("staroom","Yucicheng",$strs)," </p>"; echo "<h4>sustr_replace()</h4>"; echo "<p> 从第20位开始替换:",substr_replace($strs,"I come from Hubei,China.",20)," </p><hr />"; echo "<h3>编程3: 实例演示: usort()二维数组的排序</h3><hr />"; echo "<pre>"; $array=[[ "name"=>"张三","steps"=>20122],["name"=>"李四","steps"=>10982],["name"=>"王五","steps"=>18902]]; echo "原始:",var_dump($array),"<br />"; usort($array,function($n,$m){ return $m['steps']<$n['steps']; }); echo "步数大小到大:<br />"; var_dump($array); ?>
点击 "运行实例" 按钮查看在线实例