Blogger Information
Blog 38
fans 1
comment 0
visits 26119
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
字符串的排序和替换操作--2018年08月28日19时20分
一根火柴棒的博客
Original
501 people have browsed it

实例

<?php

$arr = ['aaa','bbb','ccc'];

//编程1: 实例演示substr(),strstr(),strpos()函数

echo substr('abcdefghijklmnopqrst',5),'</br>';//返回指定位置后面的所有字符串

$str = 'php@163.com';

//返回指定字符之前的部分
echo strstr($str,'@',true),'</br>';

//返回指定字符所在位置,索引从0开始
echo strpos('abcdefg','e'),'<hr>';


//编程2: 实例演示str_replace(), substr_replace()

//指定字符串,search中的字符被replace的字符替换
echo str_replace("%body%","black","<body text=\'%body%\'>");
echo '</br>';

//replacement替换整个字符串
echo substr_replace('fasfa#123hh','TTT',0),'</br>';

//replacement添加到整个字符串位置0处
echo substr_replace('fasfa#123hh','TTT',0,0),'<hr>';


//编程3: 实例演示: usort()二维数组的排序

$arr2 = [
    ['name'=>'Jack','id'=>3,'age'=>8],
    ['name'=>'Peter','id'=>5,'age'=>28],
    ['name'=>'Tom','id'=>2,'age'=>15],
];

echo '<pre>';
echo '排序之前数组:',var_export($arr2,true),'</br>';

usort($arr2,function($m,$n) {
    return strcmp($m['id'], $n['id']);
});

echo '排序之后数组:',var_export($arr2,true);

运行实例 »

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


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