php经典实例(字符串)

前言

php字符串是二进制安全的,(也就是说,字符串可以包含null字节),而且可以根据需要扩展和收缩,其大小只受PHP可用内存大小限制。

memory_limit = 128M

双引号字符串转义序列:https://www.php.net/manual/zh/regexp.reference.escape.php

1.访问子串

strpos — 查找字符串首次出现的位置

if(strpos('beijing','bei') !== false){
    echo 'bei in beijing';
}
// 忽视位置偏移量之前的字符进行查找
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0

2.抽取子串

substr — 返回字符串的子串

substr(string $string, int $start, int $length = ?): string

返回提取的子字符串, 或者在失败时返回 false。

扩展函数 mb_substr.

3.替换子串

substr_replace — 替换字符串的子串

$phone = '15188888888';
$string = substr_replace($phone,'****',3,4);
var_dump($string);

扩展:str_replace与substr_replace的区别:

str_replace(find,replace,string,count)

demo:

#字符串替换字符串
$stringData = str_replace('world','Admin','Hello world',$i);
print_r($stringData);//Hello Admin
echo '替换次数:'.$i;//替换次数:1

#字符串替换数组键值
$arrData = array('A','B','C','D_A');
print_r(str_replace('A','E',$arrData,$i));//Array ( [0] => E [1] => B [2] => C [3] => D_E )
echo '替换次数:'. $i;//替换次数:2

#数组匹配字符串替换数组键值
$find = array('A','D');
$replace = 'E';
$arrData = array('A','B','C','D_A');
print_r(str_replace($find,$replace,$arrData,$i));//Array ( [0] => E [1] => B [2] => C [3] => E_E )
echo '替换次数:'.$i;//替换次数:3

#数组匹配数组替换数组键值
$find = array('A','D');
$replace = array('E');
$arrData = array('A','B','C','D_A');
print_r(str_replace($find,$replace,$arrData,$i));//Array ( [0] => E [1] => B [2] => C [3] => _E )
echo '替换次数:'.$i;//替换次数:3

substr_replace(string,replacement,start,length)

#替换字符串
$a=substr_replace('ABCD1234','...',4);
print_r($a);//ABCD...

#插入字符串
$b=substr_replace('ABCD1234','...',4,0);
print_r($b);//ABCD...1234

#插入字符串和规定要替换多少个字符
$c=substr_replace('ABCD1234','...',4,1);
print_r($c);//ABCD...234

#数组匹配字符串插入
$replace = array('A','BB','CCC','DDDD');
print_r(substr_replace($replace,'E',2,0));//Array ( [0] => AE [1] => BBE [2] => CCEC [3] => DDEDD )

5.按单词或字节反转字符串

希望反转一个字符串中的单词或字节

strrev — 反转字符串

$string = "this is a dog";
$s = implode(' ',array_reverse(explode(' ',$string)));
//dog a is this

6.生成随机数

function GetRandStr($length){
    $str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $len=strlen($str)-1;
    $randstr='';
    for($i=0;$i<$length;$i++){
        $num=mt_rand(0,$len);
        $randstr .=$str[$num];
    }
    return $randstr;
}

$number=GetRandStr(6);
echo $number;

7.控制字母大小写

ucfirst — 将字符串的首字母转换为大写

$foo = 'hello world!';
$foo = ucfirst($foo);             // Hello world!

$bar = 'HELLO WORLD!';
$bar = ucfirst($bar);             // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!

ucwords — 将字符串中每个单词的首字母转换为大写

$foo = 'hello|world!';
$bar = ucwords($foo);             // Hello|world!

$baz = ucwords($foo, "|");        // Hello|World!

strtoupper — 将字符串转化为大写

$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtoupper($str);
echo $str; // 打印 MARY HAD A LITTLE LAMB AND SHE LOVED IT SO

strtolower — 将字符串转化为小写

$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // 打印 mary had a little lamb and she loved it so

8.去除字符串收尾的空格

可以使用ltrim(),rtrim()和trim()

var_dump(ltrim('10 PRINT A$','0...9'));
//打印 string(9) " PRINT A$"

9.生成逗号分隔数据

fputcsv()函数写入文件

$list = [
   [ "George,John,Thomas,USA"],
   [ "James,Adrew,Martin,USA"]
];

$file = fopen("./contacts.csv","w") or die('fopen orror');


foreach ($list as $line)
{
    fputcsv($file,$line);
}

fclose($file);

10.使文本在指定行长度自动换行

wordwrap — 打断字符串为指定数量的字串

上一篇:JS中substr与substring的区别


下一篇:python 实例