Blogger Information
Blog 19
fans 0
comment 2
visits 18496
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
20180827子串查询函数substr、strstr、strpos.usort()多维数组的排序。str_replace、atr_ireplace和substr_replace字符串替换。
乂汁的blog
Original
1255 people have browsed it

一、概述

本节课主要讲述了子串查询函数substr、strstr、strpos、usort()多维数组的排序、str_replace、atr_ireplace和substr_replace字符串替换等常用操作和函数。

二、作业部分

实例

<h3>子串查询函数substr、strstr、strpos</h3>
<?php
//1.substr、strstr、strpos
$str = 'PHP is the best language of programming';
echo 'who',substr($str,3),'?<br>';//3的位置是空格
echo strstr($str,'is',true),substr($str,3),'.<br>';
echo strstr($str,'lang',true),substr($str,15),'<br>';
echo strstr($str,'best',true),strstr($str,'best'),'<br>';
echo 'The \'e\' first occurred at No.',strpos($str, 'e'),'.<hr>';
echo '<h3>usort()多维数组的排序</h3>';
$str = [
    ['name'=>03,'age'=>04],
    ['name'=>05,'age'=>02],
    ['name'=>01,'age'=>06],
];
usort($str,function ($mm,$nn){
    return strcmp($mm['name'],$nn['name']);
});
echo '<pre>',var_export($str),'<hr>';
usort($str,function ($mm,$nn){
    return strcmp($mm['age'],$nn['age']);
});
echo var_export($str),'</pre><hr>';
echo '<h3>str_replace、atr_ireplace和substr_replace字符串替换</h3>';
$str2 = 'PHP is the best language of programming';
//echo '<pre>',var_export($str,true);
echo substr_replace($str2,'PHP是最好的编程语言in English : ',0,0), '<br>';
echo str_replace('PHP','java',$str2);
echo str_replace('PHP is the best language of programming','',$str2),'<br>';
echo str_replace(['PHP','the','is',''],'php',$str2),'<br>';
echo str_replace(['PHP','programming'],['9981','7856'],$str2),'<hr>';
echo substr_replace($str2,'PHP是最好的编程语言',0), '<br>';
echo substr_replace($str2,'PHP是最好的编程语言',0,strlen($str2)), '<br>';
echo substr_replace($str2,'是',3,3), '<br>';
echo '<br><br><br><br><br><br><br><br>';

运行实例 »

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

截图:

1.png2.png3.png

三、总结

  1. 字符串在数位置时候:正序要从0开始数,倒叙从1开始数。

  2. 在操作字符串时候,假如是从3开始,3对应的是‘p’那么是从该函数从P开始进行替换或者其他操作。


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
Author's latest blog post