Blogger Information
Blog 30
fans 0
comment 0
visits 22421
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
8.27字符串
归宿的博客
Original
649 people have browsed it

1.substr,strstr,strpos函数

<?php
/**
 * 三个最基本最常用的子串查询函数
 */
//1.substr($str,$offset,$length):只知道要获取子串的位置,精确查询
$str = 'PHP is the best programming language';
//substr(),索引从11开始的剩余内容,根据位置查询
echo substr($str,11).'<br>';
echo substr($str,11,4).'<br>';//区间查询,11开始取4个
echo substr($str,-3).'<br>';//区间查询,从尾部开始取3个

//strstr($str1,$str2,bool):
$email = 'admin@php.cn';
//查询@是否存在,默认返回@以及后面的内容
echo strstr($email,'@'),'<br>';
//传入第三个参数true,仅返回@符之前的内容(不包含@符)
echo strstr($email,'@',true),'<br>';

//strpos($str1,$str2,$start):根据内容查询,返回字符串首次出现的位置
echo strpos($str,'best');

2.str_replace(),substr_replace()

<?php
/**
 * 字符串查找并替换的两个函数
 * str_replace(),substr_replace();
 */

//1.str_replace(): 字符串替换
$str = 'peter zhu is php lecturer';
echo str_replace('php','java',$str).'<br>';
//删除替换
echo str_replace('php','',$str).'<br>';
//一次性替换多个内容
echo str_replace(['peter','zhu','php'],'朱老师',$str).'<br>';
echo str_replace(['peter','zhu','php'],['彼得','朱','讲师'],$str).'<br>';
//str_ireplace():忽略大小写



//2.substr_replace():
echo $str.'<br>';
echo substr_replace($str,'PHP是最好的编程语言',0).'<br>';//替换整个变量内容

3.usort对二维数组的排序

//usort():多维数组的排序
$stu = [
    ['name' => '林心如','grade' => 90],
    ['name' => '赵薇','grade' => 93],
    ['name' => '范冰冰','grade' => 51],
];
echo '排序前顺序:',var_export($stu).'<br>';
//用户自定义排序规则
usort($stu,function($m,$n){
    //$m,$n实际上还是一个数组
    return strcmp($m['grade'],$n['grade']);
});
echo '排序后顺序:',var_export($stu,true).'<br>';


Correction status:Uncorrected

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