您的位置 首页 php

PHP 字符串处理

1.使用sprintf来格式化 字符串

echo和print函数是输出字符串使用最普遍的函数。此外还有printf和sprintf函数。

sprintf()语法声明

sprintf(format,arg1,arg2,arg++)

format必需。规定字符串以及如何格式化其中的变量。

可能的格式值:

%% – 返回一个百分号 %

%b – 二进制数

%c – ASCII 值对应的字符

%d – 包含正负号的十进制数(负数、0、正数)

%e – 使用小写的科学计数法(例如 1.2e+2)

%E – 使用大写的科学计数法(例如 1.2E+2)

%u – 不包含正负号的十进制数(大于等于 0)

%f – 浮点数(本地设置)

%F – 浮点数(非本地设置)

%g – 较短的 %e 和 %f

%G – 较短的 %E 和 %f

%o – 八进制数

%s – 字符串

%x – 十六进制数(小写字母)

%X – 十六进制数(大写字母)

附加的格式值。必需放置在 % 和字母之间(例如 %.2f):

+ (在数字前面加上 + 或 – 来定义数字的正负性。默认情况下,只有负数才做标记,正数不做标记)

‘ (规定使用什么作为填充,默认是空格。它必须与宽度指定器一起使用。例如:%’x20s(使用 “x” 作为填充))

– (左调整变量值)

[0-9] (规定变量值的最小宽度)

.[0-9] (规定小数位数或最大字符串长度)

注释:如果使用多个上述的格式值,它们必须按照以上顺序使用。

arg1必需。规定插到 format 字符串中第一个 % 符号处的参数。

arg2可选。规定插到 format 字符串中第二个 % 符号处的参数。

arg++可选。规定插到 format 字符串中第三、四等 % 符号处的参数。

例:sprintf.php

<?php

$num = 3; //定义变量$num为3

$location = ‘desk’;//定义变量$location为desk

$format = ‘There are %d apples on the %s.<br><br>’;//定义变量$format 为There are %d apples on the %s.

printf($format, $num, $location);//格式化输出There are 3 apples on the desk.

echo sprintf($format, $num, $location);//打印There are 3 apples on the desk.

?>

结果:

%d与$num对应,%s与$location对应。

例:sprintf2.php

<?php

$num = 3;//定义变量$num为3

$location = ‘desk’;//定义变量$location为desk

$format = ‘There are %2$d apples on the %1$s.<br><br>’;//定义变量$format There are %2$d apples on the %1$s.

echo sprintf($format, $location, $num);//打印There are 3 apples on the desk.

?>

结果:

%2$d表示调用参数$num %1$s表示调用参数$location

例:sprintf3.php

<?php

$s = ‘bigmarten’;//定义变量$s为bigmarten

echo sprintf(“[%10s]<br>”,$s);//%10s表示输出宽度为10的字符串

echo sprintf(“[%-10s]<br>”,$s);//%-10s表示输出左对齐宽度为10的字符串 ‘-’为左对齐,默认右对齐

echo sprintf(“[% 10s]<br>”,$s);//% 10s表示输出宽度为10的字符串,左边空缺的部分用空格补齐

echo sprintf(“[%010s]<br>”,$s);//%010s表示输出宽度为10的字符串,左边空缺的部分用’0’补齐

echo sprintf(“[%’#10s]<br><br>”,$s);//%’#10s表示输出宽度为10的字符串,左边空缺的部分用’#’补齐

$money = 23.10;//定义变量$money为23.10

echo sprintf(“¥%5.1f<br>”, $money);//%5.1f表示输出长度为5(包括小时点),且小数点后有效位为1的浮点数

echo sprintf(“¥%.2f<br>”, $money);//%.2f表示输出小数点有效位为2的浮点数

?>

结果:

2.使用str_pad来补齐一个字符串

定义和用法

str_pad() 函数把字符串填充为新的长度。

语法

str_pad(string, length ,pad_string,pad_type)

参数描述

string必需。规定要填充的字符串。

length必需。规定新的字符串长度。如果该值小于字符串的原始长度,则不进行任何操作。

pad_string可选。规定供填充使用的字符串。默认是空白。

pad_type

可选。规定填充字符串的哪边。

可能的值:

STR_PAD_BOTH – 填充字符串的两侧。如果不是偶数,则右侧获得额外的填充。

STR_PAD_LEFT – 填充字符串的左侧。

STR_PAD_RIGHT – 填充字符串的右侧。默认。

例:str_pad.php

<?php

$str = “apple”;//定义一个字符串变量为apple

echo ‘[‘.str_pad($str, 10).’]<br>’;//填充的字符串为$str,字符串长度为10,填充使用的字符串。默认是空白

echo ‘[‘.str_pad($str, 10, “*#”, STR_PAD_LEFT).’]<br>’;//填充的字符串为$str,字符串长度为10,填充使用的字符串为*#,方向为填充字符串的左侧

echo ‘[‘.str_pad($str, 10, “*”, STR_PAD_BOTH).’]<br>’;//填充的字符串为$str,字符串长度为10,填充使用的字符串为*,方向为填充字符串的两侧。如果不是偶数,则右侧获得额外的填充。

echo ‘[‘.str_pad($str, 6, “***”).’]<br>’;//填充的字符串为$str,字符串长度为6,填充使用的字符串为*。

echo ‘[‘.str_pad($str, 3, “***”).’]<br>’;//填充的字符串为$str,字符串长度为3,如果该值小于字符串的原始长度,则不进行任何操作。

?>

结果:

3.使用strlen计算字符串长度

说明

strlen ( string $string ) : int

返回给定的字符串 string 的长度。

例:strlen.php

<?php

$str1 = ‘bigmarten’;//定义字符串变量$str1为bigmarten

echo ‘str1: ‘.strlen($str1).'<br>’;//打印变量$str1字符串长度

$str2 = ‘big marten ‘;//定义字符串变量$str2为big marten

echo ‘str2: ‘.strlen($str2).'<br>’;//打印变量$str2字符串长度

echo ‘str3: ‘.strlen($str3);//打印变量$str3字符串长度

?>

结果:

4.使用strpos来确定子串的位置

定义和用法

strpos() 函数查找字符串在另一字符串中第一次出现的位置。

语法

strpos(string,find,start)

参数描述

string必需。规定要搜索的字符串。

find必需。规定要查找的字符串。

start可选。规定在何处开始搜索。

返回值:

返回字符串在另一字符串中第一次出现的位置,如果没有找到字符串则返回 FALSE。

注释:字符串位置从 0 开始,不是从 1 开始。

例:strpos.php

<?php

$str = ‘aple’;//定义一个字符串变量$str为apple

$needle = ‘p’;//定义一个字符串变量$needle为p

$pos = strpos($str, $needle);//定义一个变量$pos来存储子串p在apple中位置

if ($pos === false) { //如若变量$pos不存在

echo “‘$needle’ is not in ‘$str’.<br><br>”;//打印p is not in apple

} else {

echo “The position is $pos.<br><br>”;//存在打印The position is 1.

}

$pos = strpos($str, ‘p’, 2); //定义一个变量$pos来存储子串p在apple中从第2个位置开始查找位置

if ($pos === false) {//如若变量$pos不存在

echo “‘$needle’ is not in ‘$str’.”;//打印p is not in apple

} else {

echo “The position is $pos.”;//存在打印The position is 2.

}

?>

结果:

5.使用stripos来确定子串的位置

不区分大小写用stripos

语法

stripos(string,find,start)

参数描述

string必需。规定要搜索的字符串。

find必需。规定要查找的字符。

start可选。规定开始搜索的位置。

返回值:

返回字符串在另一字符串中第一次出现的位置,如果没有找到字符串则返回 FALSE。

注释:字符串位置从 0 开始,不是从 1 开始。

例:stripos.php

<?php

$needle = ‘a’;//定义一个字符串变量$needle为a

$str = ‘Apple’;//定义一个字符串变量$str为apple

$pos = strpos($str, $needle);//定义一个变量$pos来存储子串a在apple中位置

if ($pos === false) {//如若变量$pos返回值为false

echo “‘$needle’ is not in ‘$str’.<br><br>”;//打印a is not in apple

}

else{

echo “The position is $pos.<br><br>”;//存在打印The position is $pos.

}

$pos = stripos($str, $needle);

if ($pos === false) {//如若变量$pos返回值为false

echo “‘$needle’ is not in ‘$str’.”;//打印a is not in apple

}

else{

echo “The position is $pos.”;//存在打印The position is $pos.

}

?>

结果:

6.使用substr来返回子串

定义和用法

substr() 函数返回字符串的一部分。

注释:如果 start 参数是负数且 length 小于或等于 start,则 length 为 0。

语法

substr(string,start,length)

参数描述

string必需。规定要返回其中一部分的字符串。

start

必需。规定在字符串的何处开始。

正数 – 在字符串的指定位置开始

负数 – 在从字符串结尾开始的指定位置开始

0 – 在字符串中的第一个字符处开始

length

可选。规定被返回字符串的长度。默认是直到字符串的结尾。

正数 – 从 start 参数所在的位置返回的长度

负数 – 从字符串末端返回的长度

例:substr.php

<?php

echo substr(‘apple’, 1).'<br>’; //从1的位置开始打印子串pple

echo substr(‘apple’, 1, 3).'<br>’;//从1的位置开始打印3个长度子串ppl

echo substr(“apple”, -2).'<br>’; //负数 – 在从字符串结尾开始的指定位置左2开始打印子串le

echo substr(“apple”, -3, 1).'<br>’;//负数 – 在从字符串结尾开始的指定位置左3开始打印1个子串p

echo substr(“apple”, 2, -1).'<br>’;//正数 – 在字符串的指定位置开始pl

echo substr(“apple”, -2, -1).'<br>’;//l

echo substr(“apple”, 4, -1).'<br>’;//空

?>

结果:

7.使用str_replace替换字符串

语法

str_replace(find,replace,string,count)

参数描述

find必需。规定要查找的值。

replace必需。规定替换 find 中的值的值。

string必需。规定被搜索的字符串。

count可选。对替换数进行计数的变量。

例:str_replace.php

<?php

$subject = “apple banana and orange.”;//定义一个字符串变量$subject为apple banana and orange.

$old = array(“apple”, “banana”, “orange”);//定义要被替换数组为$old

$new = array(“book”, “pen”, “desk”);//定义要替换数组为$new

echo str_replace($old, $new, $subject).'<br><br>’;//打印用数组$new替换$old在$subject形式中,数组的元素一一对应

$str = str_replace(“p”, “*”, “apple”, $count);//用“*”替换apple中的“p”,count为替换次数

echo ‘替换的次数: ‘.$count;//打印替换的次数

?>

结果:

8.字符串比较函数 strcmp 与strncmp

1)strcmp

strcmp() 函数比较两个字符串。注释:strcmp() 函数是二进制安全的,且对大小写敏感。

语法

strcmp(string1,string2)

参数描述

string1必需。规定要比较的第一个字符串。

string2必需。规定要比较的第二个字符串。

返回值:

本函数返回:

0 – 如果两个字符串相等

<0 – 如果 string1 小于 string2 (ASCII码值比较)

>0 – 如果 string1 大于 string2

例:strcmp.php

<?php

$str1 = ‘abc’;//定义一个字符串变量$str1 为abc

$str2 = ‘aBcde’;//定义另一个字符串变量$str2 为aBcde

if(strcmp($str1,$str2) > 0) //>0 ASCII码值比较大小

echo “‘$str1’ > ‘$str2′”;//打印’abc’ > ‘aBcde’

else if(strcmp($str1,$str2) < 0)//<0

echo “‘$str1’ < ‘$str2′”;//打印’abc’ < ‘aBcde’

else

echo “‘$str1’ == ‘$str2′”;//打印’abc’ = ‘aBcde’

?>

结果:

注:从左到右依次比较 由于ASCII b>B所以打印’abc’ > ‘aBcde’

2)strncmp

定义和用法

strncmp() 函数比较两个字符串。

注释:strncmp() 是二进制安全的,且对大小写敏感。

提示:该函数与 strcmp() 函数类似,不同的是,strcmp() 没有 length 参数。

语法

strncmp(string1,string2,length)

参数描述

string1必需。规定要比较的首个字符串。

string2必需。规定要比较的第二个字符串。

length必需。规定比较中所用的每个字符串的字符数。

返回值:

本函数返回:

0 – 如果两个字符串相等

<0 – 如果 string1 小于 string2

>0 – 如果 string1 大于 string2

例:strncmp.php

<?php

$str1 = ‘abc’;//定义一个字符串变量$str1 为abc

$str2 = ‘aBcde’;//定义另一个字符串变量$str2 为aBcde

if(strncmp($str1,$str2,1) > 0)//只取一个字符比较 ‘a’ == ‘a’

echo “‘$str1’ > ‘$str2′”;//打印’abc’ > ‘aBcde’

else if(strncmp($str1,$str2,1) < 0)

echo “‘$str1’ < ‘$str2′”;//打印’abc’ < ‘aBcde’

else

echo “‘$str1’ == ‘$str2′”;//打印’abc’ == ‘aBcde’

?>

结果:

9.不区分大小写的比较函数strcasecmp与strncasecmp

1)strcasecmp

定义和用法

strcasecmp() 函数比较两个字符串。功能和strcmp相同。

提示:strcasecmp() 函数是二进制安全的,且不区分大小写。

提示:该函数与 strncasecmp() 函数类似,不同的是,通过 strncasecmp() 您可以指定每个字符串用于比较的字符数。

语法

strcasecmp(string1,string2)

参数描述

string1必需。规定要比较的第一个字符串。

string2必需。规定要比较的第二个字符串。

返回值:

该函数返回:

0 – 如果两个字符串相等

<0 – 如果 string1 小于 string2

>0 – 如果 string1 大于 string2

例:strcasecmp.php

<?php

$str1 = “apple”;//定义一个字符串变量$str1 为apple

$str2 = “aPPle”;//定义另一个字符串变量$str2 为aPPle

if (strcasecmp($str1, $str2) == 0) {//==0 ASCII码值比较大小

echo “‘$str1’ == ‘$str2’.”;//打印”apple” == “aPPle”

}

else if(strcasecmp($str1, $str2) > 0) {//>0 ASCII码值比较大小

echo “‘$str1’ > ‘$str2’.”;//打印”apple” > “aPPle”

}

else{

echo “‘$str1’ < ‘$str2’.”;//打印”apple” < “aPPle”

}

?>

结果:

2)strncasecmp

定义和用法

strncasecmp() 函数比较两个字符串。

注释:strncasecmp() 是二进制安全的,对大小写不敏感。

提示:该函数与 strcasecmp() 函数类似,不同的是,strcasecmp() 没有 length 参数。

语法

strncasecmp(string1,string2,length)

参数描述

string1必需。规定要比较的第一个字符串。

string2必需。规定要比较的第二个字符串。

length必需。规定每个字符串用于比较的字符数。

返回值:

本函数返回:

0 – 如果两个字符串相等

<0 – 如果 string1 小于 string2

>0 – 如果 string1 大于 string2

例:strncasecmp.php

<?php

$str1 = “appl”;//定义一个字符串变量$str1 为apple

$str2 = “aPPle”;//定义另一个字符串变量$str2 为aPPle

if (strncasecmp($str1,$str2,5) == 0) {//==0 ASCII码值比较5个字符的大小

echo “‘$str1’ == ‘$str2’.”;//打印”apple” == “aPPle”

}

else if(strncasecmp($str1,$str2,5) > 0) {

echo “‘$str1’ > ‘$str2’.”;//打印”apple” > “aPPle”

}

else{

echo “‘$str1’ < ‘$str2’.”;//打印”apple” < “aPPle”

//echo strlen($str1);//打印$str1字符串长度

}

?>

结果:

10.大小写转换函数strtoupper与strtolower

1)strtoupper

定义和用法

strtoupper() 函数把字符串转换为大写。

注释:该函数是二进制安全的。

语法

strtoupper(string)

参数描述

string必需。规定要转换的字符串。

返回值:返回转换为大写的字符串。

例:strtoupper.php

<?php

$str = “Apple#is@sweat.”;//定义一个字符串变量$str为Apple#is@sweat.

$str = strtoupper($str);//返回转换为大写的字符串

echo $str;//打印$str变量

?>

结果:

2)strtolower

定义和用法

strtolower() 函数把字符串转换为小写。

注释:该函数是二进制安全的。

语法

strtolower(string)

参数描述

string必需。规定要转换的字符串。

返回值:返回转换为小写的字符串。

例:strtolower.php

<?php

$str = “APPLE#IS@Sweat.”;//定义一个字符串变量$str为Apple#is@sweat.

$str = strtolower($str);//返回转换为小写的字符串。

echo $str;//打印$str变量

?>

结果:

11.首字母大写转换函数ucfirst与ucwords

1)ucfirst

定义和用法

ucfirst() 函数把字符串中的首字符转换为大写。

相关函数:

lcfirst() – 把字符串中的首字符转换为小写

语法

ucfirst(string)

参数描述

string必需。规定要转换的字符串。

返回值:返回已转换的字符串。

例:ucfirst.php

<?php

$str1 = ‘apple’;//定义一个字符串变量$str1为apple

$str1 = ucfirst($str1);//把字符串中的首字符转换为大写Apple

echo $str1.'<br><br>’;//打印变量$str1 Apple

$str2 = ‘aPPle’;//定义一个字符串变量$str2为aPPle

$str2 = ucfirst(strtoupper(strtolower($str2)));//先转换成小写在转化成大写,最后首字母大写

echo $str2;//打印变量$str2 APPLE

?>

结果:

2)ucwords

定义和用法

ucwords() 函数把字符串中每个单词的首字符转换为大写。

注释:该函数是二进制安全的。

语法

ucwords(string)

参数描述

string必需。规定要转换的字符串。

返回值:返回已转换的字符串。

例:ucwords.php

<?php

$str1 = ‘apple banana’;//定义一个字符串变量$str1为apple banana

$str1 = ucwords($str1);//把字符串中每个单词的首字符转换为大写。Apple Banana

echo $str1.'<br><br>’;//打印变量$str1 Apple Banana

$str2 = ‘APPLE BANANA’;//定义一个字符串变量$str2为APPLE BANANA

$str2 = ucwords($str2);//把字符串中每个单词的首字符转换为大写

echo $str2;//打印变量$str2 APPLE BANANA

?>

结果:

12. 字符串的切分函数 explode

定义和用法

explode() 函数把字符串打散为数组。

注释:”separator” 参数不能是空字符串。

注释:该函数是二进制安全的。

语法

explode(separator,string,limit)

参数描述

separator必需。规定在哪里分割字符串。

string必需。要分割的字符串。

limit

可选。规定所返回的数组元素的数目。

可能的值:

大于 0 – 返回包含最多 limit 个元素的数组

小于 0 – 返回包含除了最后的 -limit 个元素以外的所有元素的数组

0 – 返回包含一个元素的数组

返回值:返回字符串的数组

例:

1)explode.php

<?php

$data = “”; //定义一个字符串变量$data 为

list($http, $name, $ext) = explode(“.”, $data);//用’.’切分 分三部分 csdn net

list($http, $www) = explode(“://”, $http);////用’://’切分 分两部分http www

echo $http.'<br><br>’;//http

echo $www.'<br><br>’;//www

echo $name.'<br><br>’;//cndn

echo $ext.'<br><br>’;//net

?>

结果:

2)explode2.php

<?php

$str = ‘1,2,3,4,5’;//定义一个字符串变量$str 为1,2,3,4,5

echo ‘< pre >’;//格式化形式

print_r($lists = explode(‘,’, $str, 2));//打印 按’,’切分2(正数2)部分 1 2,3,4,5

echo ‘</pre><pre>’;

print_r($lists = explode(‘,’, $str, -2));//打印 按’,’ 切分 1 2 3 (-2 为负数,除去末尾两个都按这规则切分)

echo ‘</pre>’;

foreach($lists as $list) { //用遍历方式循环找符合规则的数相加

$list += $list;

}

echo “explode(‘,’, ‘$str’, -2)分割后的元素之和:”.$list;//1+2+3 =6

?>

结果:

13. 字符串合并函数implode

定义和用法

implode() 函数返回由数组元素组合成的字符串。

注释:implode() 函数接受两种参数顺序。但是由于历史原因,explode() 是不行的,您必须保证 separator 参数在 string 参数之前才行。

注释:implode() 函数的 separator 参数是可选的。但是为了向后兼容,推荐您使用使用两个参数。

注释:该函数是二进制安全的。

语法

implode(separator,array)

参数描述

separator可选。规定数组元素之间放置的内容。默认是 “”(空字符串)。

array必需。要组合为字符串的数组。

返回值:返回由数组元素组合成的字符串。

例:implode.php

<?php

$array = array(‘www’, ‘csdn’, ‘net’);//定义一个数组$array

$http = implode(“.”, $array);//用’.’的规则将每个数组元素进行合并

echo $http; //打印新的变量$http

?>

结果:

14.函数str_split平均分割字符串

定义和用法

str_split() 函数把字符串分割到数组中。

语法

str_split(string,length)

参数描述

string必需。规定要分割的字符串。

length可选。规定每个数组元素的长度。默认是 1。

技术细节

返回值:

如果 length 小于 1,则 str_split() 函数将返回 FALSE。

如果 length 大于字符串的长度,则整个字符串将作为数组的唯一元素返回。

例:str_split.php

<?php

$str = “apple banana”;//定义一个字符串变量$str为apple banana

$str1 = str_split($str);//平均分割变量$str赋值给数组$str1

$str2 = str_split($str, 3);//按3个字符平均分割变量$str赋值给数组$str2

echo “<pre>”;

print_r($str1);//打印数组$str1

echo “</pre><pre>”;

print_r($str2);//打印数组$str2

echo “</pre>”;

?>

结果:

15.在字符串中包含变量,单引号与双引号的区别

当字符串中含有变量时,应使用双引号输出。如果直接输出字符串中的内容,可以使用单引号。

例:str.php

<?php

$food = ‘apple’;//定义变量$food为apple

$person = ‘Tom’;//定义变量$person为Tom

$str1 = ‘$person eat $food.’;//定义变量$str1为$person eat $food.

$str2 = “$person eat $food.”;//定义变量$str2为Tom eat apple.

$str3 = ‘”$person” eat “$food”.’;//定义变量$str3为”$person” eat “$food”.

$str4 = “‘$person’ eat ‘$food’.”;//定义变量$str4为’Tom’ eat ‘apple’.

echo $str1.'<br><br>’;//打印变量$str1

echo $str2.'<br><br>’;//打印变量$str2

echo $str3.'<br><br>’;//打印变量$str3

echo $str4.'<br><br>’;//打印变量$str4

?>

结果:

文章来源:智云一二三科技

文章标题:PHP 字符串处理

文章地址:https://www.zhihuclub.com/76548.shtml

关于作者: 智云科技

热门文章

网站地图