Blogger Information
Blog 35
fans 0
comment 0
visits 32653
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
子字符串查询、替换函数-多维数组的排序——2018-9-2
THPHP
Original
718 people have browsed it

1、子字符串查询函数:

实例

<?php
$str = 'All crows under the sun are black tianhong@.com';
// 从$str字符串中,第10个索引开始,取出四个字符
echo substr($str,10,4),'<br>';
// 查找出$str字符中的 @字符,并且@后面的内容取出
echo strstr($str,'@'),'<br>';
// 查找出$str字符中的 @字符,并且@前面的内容取出,不包含@
echo strstr($str,'@',true),'<br>';
// 查找出$str字符中的 under字符,并且把under的首字母索引取出
echo strpos($str,'under');// 10

运行实例 »

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

2、字符串查找并替换:

实例

<?php
$str = 'All crows under the sun are black tianhong@.com';
 // 把$str变量中的 tianhong 字符 替换成 tian
echo str_replace('tianhong','tian',$str),'<br>';
 // 把天弘字符插入到 $str变量字符的 0 索引位置,并且取出原有的 10个字符,一起打印
echo substr_replace($str,'天弘',0,10);
 // 如果给一个参数,就只能打印出天弘二字,原有的字符没了
echo substr_replace($str,'天弘',0);

运行实例 »

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

3、多维数组的排序:

实例

<?php
$stu = [
    ['name' => '依依','ID' => 8],
    ['name' => '哒哒','ID' => 6],
    ['name' => '痛痛','ID' => 9],
    ['name' => '哈哈','ID' => 5],
    ['name' => '咯咯','ID' => 2]
];
usort($stu,function ($m,$n){
    // 比较 Id的值,进行排序,值的范围:0~9
    return strcmp($m['ID'],$n['ID']);
});
echo '<pre>';
echo var_export($stu);

运行实例 »

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


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