数组排序:
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>数组排序</title> </head> <body> <pre> <?php //原数组 $sort_a=array("D","A","E","F","B","C"); print_r($sort_a); echo "<hr color='red'>"; //sort方法排序,对数值数组元素按照字母顺序进行升序排列 sort($sort_a); print_r($sort_a); echo "<hr color='red'>"; //sort方法排序,对数值数组元素按照字母顺序进行降序排列 $sort_a=array("D","A","E","F","B","C"); rsort($sort_a); print_r($sort_a); echo "<hr color='red'>"; //asort方法排序,对数值数组元素按照字母顺序进行降序排列 //原数组 $age=array("Peter"=>"44","Ben"=>"32","Joe"=>"47"); print_r($age); echo "<hr color='red'>"; //asort方法排序,根据数组的值,对关联数组进行升序排列 asort($age); print_r($age); echo "<hr color='red'>"; //arsort方法排序,根据数组的值,对关联数组进行降序排列 $age=array("Peter"=>"44","Ben"=>"32","Joe"=>"47"); arsort($age); print_r($age); echo "<hr color='red'>"; //ksort方法排序,根据键名的值,对关联数组进行升序排列 $age=array("Peter"=>"44","Ben"=>"32","Joe"=>"47"); ksort($age); print_r($age); echo "<hr color='red'>"; //krsort方法排序,根据键名的值,对关联数组进行降序排列 $age=array("Peter"=>"44","Ben"=>"32","Joe"=>"47"); krsort($age); print_r($age); echo "<hr color='red'>"; ?> </pre> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
字符串长度计算:
实例
<?php /** * @Author: Marte * @Date: 2018-04-19 11:19:55 * @Last Modified by: Marte * @Last Modified time: 2018-04-19 11:24:49 */ //字符串长度计算,英文占用一个字符,中文占用三个字符 echo strlen(" Hello world! ").',英文占用一个字符'; echo('<hr color="red">'); echo strlen("你好,世界!").',中文占用三个字符';
运行实例 »
点击 "运行实例" 按钮查看在线实例
字符查找和替换:
实例
<meta charset="UTF-8" /> <?php echo '<pre>'; //strpos()查找hello world,出现位置在第六个 echo strpos("Hello world!","world"); echo '<hr color="red">'; // str_replace(find,replace,string,count)替换字符 $str="welcome to beijing!"; echo str_replace("beijing","anhui",$str); echo '<hr color="red">'; echo '</pre>';
运行实例 »
点击 "运行实例" 按钮查看在线实例
字符串转换:
实例
<meta charset="UTF-8" /> <?php /** * @Author: Marte * @Date: 2018-04-19 12:01:50 * @Last Modified by: Marte * @Last Modified time: 2018-04-19 12:42:11 */ //str_split字符串分割 echo '<pre>'; $a="hello"; echo('原字符串是:'.$a.'。'.'<br>'); echo '<hr color="red">'; echo('分割为数组后:'.'<br>'); print_r(str_split($a)); echo '<hr color="red">'; echo('以三个为组分割数组后:'.'<br>'); print_r(str_split($a,3)); echo '<hr color="red">'; //explode字符串分割 $b = 'one,two,three,four'; echo('原字符串是:'.$b.'。'.'<br>'); // 分割为1个数组 print_r(explode(',',$b,0)); // 分割为4个数组 print_r(explode(',',$b,4)); // 去除最后1个字符串 print_r(explode(',',$b,-1)); echo '<hr color="red">'; // 把数组元素组合为一个字符串 $c=array("hell","world"); //中间为空字符串 echo implode(" ",$c).'<br>'; //中间为, echo implode(",",$c); echo '</pre>';
运行实例 »
点击 "运行实例" 按钮查看在线实例