PHP比较常用的字符串截取函数

您所在的位置:网站首页 php中截取字符串的函数怎么用 PHP比较常用的字符串截取函数

PHP比较常用的字符串截取函数

2024-07-05 20:56| 来源: 网络整理| 查看: 265

substr(字符串,开始,长度)​​​​​​。

public function test2(){ $str = '123456789abcdefg'; echo substr($str,1,10); } //echo 23456789ab

strpos(字符串,查找内容,开始位置(可选)) -- 查找字符串在另一字符串中第一次出现的位置(区分大小写)。

public function test3(){ $str = '112aA233445566778899'; echo strpos($str,'a'); } //echo 3

stripos(字符串,查找内容,开始位置(可选)) -- 查找字符串在另一字符串中第一次出现的位置(不区分大小写)。

public function test4(){ $str = '1122AaA33445566778899'; echo stripos($str,'a'); } //echo 4

strripos(字符串,查找内容,开始位置(可选)) -- 查找字符串在另一字符串中最后一次出现的位置(不区分大小写)。

public function test4(){ $str = '1122AaA33445566778899'; echo strripos($str,'a'); } //echo 6

str_split(字符串,每个字符串长度(可选)) -- 把字符串分割到数组中。

public function test5(){ $str = '112233'; print_r(str_split($str)); } //echo Array([0] => 1 [1] => 1 [2] => 2 [3] => 2 [4] => 3 [5] => 3) public function test6(){ $str = '112233'; print_r(str_split($str,2)); } //echo Array([0] => 11 [1] => 22 [2] => 33)

explode(分隔符,字符串,规定所返回的数组元素的数目)  -- 把字符串拆分成数组。

public function test7(){ $str = "www.baidu.com"; print_r (explode(".",$str)); } //echo Array([0] => www [1] => baidu [2] => com)

 平时开发过程中经常遇到的一些截取字符串特定内容的例子: 

public function test8(){ $str = '11(22)33'; echo substr($str,strrpos($str,"("),strrpos($str,")") - 1); } //echo (22)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3