Blogger Information
Blog 17
fans 0
comment 0
visits 11921
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示substr(),strstr(),strpos()函数/实例演示str_replace(), substr_replace()/实例演示: usort()二维数组的排序 2018-08-28 17:00
Aken的博客
Original
583 people have browsed it

实例

<?php
/**
 * 实例演示substr(),strstr(),strpos()函数
 */

//substr() 返回字符串中的一部分,精确查找
$str = 'i like php';
$str1 = substr($str,2,4);
echo $str1.'<br>';
$str2 = substr($str,-3,1);
echo $str2.'<br>';


//strstr();函数搜索字符串在另一字符串中的第一次出现
$str3 = strstr($str,'k');   //返回k及之后的内容
$str4 = strstr($str,'k',true);   //返回K之前的内容
echo $str3.'<br>';
echo $str4.'<br>';

//strpos();根据内容查找,返回字符串首次出现的位置
$str5 = strpos($str,'i');
echo $str5;

/**
*实例演示str_replace(), substr_replace()
 */
echo '<hr>';
//str_replace(); 函数以其他字符替换字符串中的一些字符(区分大小写)
$str6 = str_replace('i','you',$str);
echo $str6.'<br>';


//substr_replace();
echo $str.'<br>';
echo strlen($str);
$str7 = substr_replace($str,'中国上海','2');
echo $str7;

/**
*实例演示: usort()二维数组的排序
 */
$arr = [['id'=>2,'name'=>'bob'],['id'=>3,'name'=>'hair'],['id'=>1,'name'=>'jake']];
echo '<pre>';
echo '原数组:',var_export($arr);
echo '<br>';
usort($arr,function($a,$b){
    if($a['id'] == $b['id']) return 0;
    return $a['id']<$b['id']?-1:1;
});

echo '排序后的数组:',var_export($arr);

运行实例 »

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


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