Blogger Information
Blog 38
fans 0
comment 0
visits 29633
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0418,数组、字符串操作函数
riskcn的博客
Original
710 people have browsed it

实例

<?php 
header("Content-type: text/html; charset=utf-8");
$arr=["id"=>30,"name"=>"老王","age"=>40,"salary"=>5000,5=>"teacher"];
echo('<pre>');
print_r($arr);
echo('<hr color="red">');

// 排序变量
 /*	1.SORT_REGULAR: [默认]正常比较单元(不改变类型)
 *  2.SORT_NUMERIC: 单元被作为数字来比较
 *  3.SORT_STRING: 单元被作为字符串来比较
 *  4.SORT_NATURAL: 以“自然的顺序”对字符串进行排序
 *  5.SORT_FLAG_CASE: 不区分大小写排序字符串*/

// sort()排序==忽略键名,按照顺序给与键名
sort($arr);
print_r($arr);
//rsort()排序,顺序和sort()方式相反;
echo('<hr>');
rsort($arr);
print_r($arr);
echo ('<hr>');
//asort()和sort()相比,保留了键名;
$arr1=["id"=>30,"name"=>"老王","age"=>40,"salary"=>5000,5=>"teacher"];
asort($arr1);
print_r($arr1);
//arsort()排序,顺序和asort()方式相反;
echo('<hr>');
arsort($arr1);
print_r($arr1);
echo ('<hr>');
//ksort()效果是以键名顺序排序
ksort($arr1);
print_r($arr1);
echo ('<hr>');
//krsort()效果是以键名倒叙排序
krsort($arr1);
print_r($arr1);


echo('<hr color="green">');
//字符串长度计算
$str="我在php.cn学习PHP";
// 内部编码
$encoding=mb_internal_encoding();
echo $encoding."<br>";
echo $str."<br>";
//utf-8编码一个中文字符占3个字符长度
echo strlen($str)."<br>";

echo mb_strlen($str,$encoding);
echo ('<hr color="green">');
//字符串切割和拼接
$date="2018-04-18";
//字符串切割为数组
print_r(explode("-", $date));
$date2=explode("-", $date);
// 数组组合为字符串
print_r(implode("/",$date2));
echo ('<hr color="red">');

//字符串查找和替换
$str1="www.php.cn";
echo(strpos($str1,'p'));//寻找字符串中某字符最先出现的位置

echo(strstr($str1,'php'));//返回一个字符串在另一个字符串中开始位置到结束的字符串

echo(str_replace("php","java",$str1))//字符串替换操作,区分大小写

 ?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post