Blogger Information
Blog 56
fans 3
comment 1
visits 50861
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP字符串——2018年4月19日
沈斌的博客
Original
634 people have browsed it

PHP字符串的查找替换,分割合并:


实例

<?php
	$s='helloworld!';
	echo strlen($s),'<br>';
	echo mb_strlen($s,"UTF-8");

	// 字符串与数组转换
	$str='html,js,php,jquery';
	echo "<pre>";
	// str_split
	print_r(str_split($str));

	// explode
	print_r(explode(',',$str));
	// 指定分割的长度
	print_r(explode(',',$str,3));

	// implode,返回字符串
	$arr1=explode(',', $str);
	echo implode(',',$arr1),'<br>';
	echo implode('*',$arr1);


	echo "<hr>";
	// 字符串的查找替换
	$arr='www.baidu.com';

	echo strpos($arr,'d'),'<br>';

	echo strstr($arr,'du'),'<br>';
	// 返回子串前面部分
	echo strstr($arr,'du',true),'<br>';

	echo str_replace('du','dduu',$arr),'<br>';

	// 第4个索引起三个字符替换为hello
	echo substr_replace($arr,'hello',4,3);

运行实例 »

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

数组排序:


实例

<?php
	$arr=['id'=>4,'name'=>"pet",'family'=>'js','grade'=>70,7=>true];

	echo "<pre>";

	echo "<p>未排序数组</p>";
	print_r($arr);

	echo "<hr>";

	// sort($arr);

	// 数值类型排序
	// sort($arr,SORT_NUMERIC);
	// 字符类型排序
	sort($arr,SORT_STRING);
	print_r($arr);

	echo "<hr>";
	// 保留键名
	$arr=['id'=>4,'name'=>"pet",'family'=>'js','grade'=>70,7=>true];
	asort($arr);
	print_r($arr);

	echo "<hr>";
	$arr=['id'=>4,'name'=>"pet",'family'=>'js','grade'=>70,7=>true];
	// 对键名排序
	ksort($arr);
	print_r($arr);

	echo "<br>";
	$arr=['id'=>4,'name'=>"pet",'family'=>'js','grade'=>70,7=>true];

	// 反转排序
	rsort($arr,SORT_NUMERIC);
	// arsort($arr,SORT_STRING);
	// krsort($arr,SORT_STRING);
	print_r($arr);

	echo "<hr>";
	// 自定义排序
	$arr1=[10,4,55,3,22,99];
	usort($arr1,function($a,$b){
		if ($a>$b) {
			return false;
		} else if ($a == $b){
			return 0;
		} else {
			return true;
		}
	});
	print_r($arr1);

运行实例 »

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

arr.png

str.png

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