Blogger Information
Blog 12
fans 0
comment 0
visits 10509
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
substr(),strstr(),strpos()str_replace(), substr_replace() usort()函数8月28日17:32
A骆长林的博客
Original
772 people have browsed it

实例

<meta charset="utf-8">
<?php
echo "<pre>";
//usort()二维数组的排序
$stu = [
    ['name'=>'林心如','grade'=>98],
    ['name'=>'范冰冰','grade'=>55],
    ['name'=>'左小青','grade'=>73],
];
echo '排序前'.var_export($stu).'<hr>';
usort($stu,function ($m,$n){
    return strcmp($m['grade'],$n['grade']);
});
echo var_export($stu).'<hr>';

//str_replace(), substr_replace()
//str_replace()替换Mothers  内容
$str ='As Mothers Day is coming soon.';
echo $str.'<br>';
echo str_replace('Mothers','haha',$str).'<br>';

//str_replace()删除替换
echo str_replace('Mothers','',$str).'<br>';

$str1 = 'Peter Zhu is PHP Lecture';
echo $str1.'<hr><br>';
// 一次性替换多个内容
echo str_replace(['Peter','Zhu','PHP'],'朱老师', $str1), '<br>';
echo str_replace(['Peter','Zhu','Lecture'],['彼得','朱','讲师'], $str1), '<br>';

// str_ireplace(): 忽略大小写的替换
echo str_ireplace(['peter','php'],'laoshi',$str1).'<br>';

//substr_replace()
echo substr_replace($str1,'我正在学习php',0).'<br>';  //内容全部替换
echo substr_replace($str1,'我正在学习php',0,strlen($str1)).'<br>';//内容全部替换

//开始是从0开始数 length是替换几个字符
echo substr_replace($str1, 'PHP中文网  ',0,0),'<br>';
echo substr_replace($str1, 'JAVA',13,3),'<br>';

// 删除式替换
echo substr_replace($str1, '',6,3),'<br>';
echo '<br><br><hr>';

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

//strstr($str1, $str2,bool)
$email1 ='luo@php.cn';
echo $email1.'<br>';
echo strstr($email1,'@').'<br>'; //输出@以后的内容,@php.cn
// 传入第三个参数:true,仅返回@符之前的内容(不包含@)
echo strstr($email1, '@',true),'<br>';
//用strstr拼接
echo strstr($email1, '@',true),strstr($email1, '@'),'<br>';

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

运行实例 »

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


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