PHP函数
1、strlen()函数返回字符串长度
<?php echo strlen("hello") ?>
2、对字符串中的单词计数
<?php echo str_word_count("do you know this"); ?>
3、反转字符串
<?php echo strrev("do you know this"); ?>
4、替换字符串中文本
<?php echo str_replace("this","that","do you know this"); ?>
5、对数组排序函数
sort() - 以升序对数组排序
rsort() - 以降序对数组排序
asort() - 根据值,以升序对关联数组进行排序
ksort() - 根据键,以升序对关联数组进行排序
arsort() - 根据值,以降序对关联数组进行排序
krsort() - 根据键,以降序对关联数组进行排序
例1:按字母升序/降序
<?php $fruits=array("Banana","peach","apple"); sort($fruits); $clength=count($fruits); for($x=0;$x<$clength;$x++) { echo $fruits[$x]; echo "<br>"; } ?>
<?php $fruits=array("Banana","peach","apple"); rsort($fruits); $clength=count($fruits); for($x=0;$x<$clength;$x++) { echo $fruits[$x]; echo "<br>"; } ?>
例2:按数字升序/降序
<?php $fruits=array(7,1,22,13); sort($fruits); $clength=count($fruits); for($x=0;$x<$clength;$x++) { echo $fruits[$x]; echo "<br>"; } ?>
<?php $fruits=array(7,1,22,13); rsort($fruits); $clength=count($fruits); for($x=0;$x<$clength;$x++) { echo $fruits[$x]; echo "<br>"; } ?>
例3:根据值对数组进行升序/降序
<?php $age=array("ann"=>"30","tian"=>"18","bill"=>"26"); asort($age); foreach($age as $x=>$x_value) { echo "key=" . $x . ", value=" . $x_value; echo "<br>"; } ?>
<?php $age=array("ann"=>"30","tian"=>"18","bill"=>"26"); arsort($age); foreach($age as $x=>$x_value) { echo "key=" . $x . ", value=" . $x_value; echo "<br>"; } ?>
例4:根据键对数组进行升序/降序
<?php $age=array("ann"=>"30","tian"=>"18","bill"=>"26"); ksort($age); foreach($age as $x=>$x_value) { echo "key=" . $x . ", value=" . $x_value; echo "<br>"; } ?>
<?php $age=array("ann"=>"30","tian"=>"18","bill"=>"26"); krsort($age); foreach($age as $x=>$x_value) { echo "key=" . $x . ", value=" . $x_value; echo "<br>"; } ?>