本文实例讲述了PHP常见字符串操作函数与用法。分享给大家供大家参考,具体如下:
一、字符串的格式化
1、字符串的格式化
trim()
函数可以去除字符串的开始位置和结束位置的空格,并将结果字符串返回,默认情况下去除的字符是换行符和回车符(\\n和\\r),水平和垂直制表符(\\t和X0B)
ltrim()
函数只从字符的开始处(左边)去除空格
rtrim()
函数只从函数的结束处(右边)去除空格
2、格式化字符串以便显示
①使用HTML格式化:n12br()
函数
在字符串中的新行(\\n)之前插入换行符
<?php echo nl2br(\"One line.\\nAnother line.\"); ?>
结果
One line.
Another line.
②为打印输出而格式化字符串
printf()
结构
$s=\"world\"); printf(\"Hello %s\",$s);
3.改变字符串中的字母大小写
函数 | 描述 | 使用 $subject=Hello World | 返回值 |
---|---|---|---|
strtoupper() | 将字符串转为大写 | strtoupper($subject ) | HELLO WORLD |
strtolower() | 将字符串转为小写 | strtolower($subject ) | hello world |
ucfirst() | 如果字符串第一个字符是字符,将其转为大写 | ucfirst($subject ) | Hello world |
ucwords() | 将字符串的每个单词的首字母大写 | ucwords($subject ) | Hello World |
二、用字符串函数连接和分割字符串
1、用函数explode()、implode()和join()
exlpode()
把字符串打散为数组:
<!DOCTYPE html> <html> <body> <?php $str = \"Hello world. I love Shanghai!\"; print_r (explode(\" \",$str)); ?> </body> </html>
结果
Array ( [0] => Hello [1] => world. [2] => I [3] => love [4] => Shanghai! )
implode()
(jion()
是implode()
函数的别名)
把数组元素组合为字符串:
<!DOCTYPE html> <html> <body> <?php $arr = array(\'Hello\',\'World!\',\'I\',\'love\',\'Shanghai!\'); echo implode(\" \",$arr); ?> </body> </html>
结果
Hello World! I love Shanghai!
2、使用strtok()函数
strtok()
函数把字符串分割为更小的字符串(标记)。
语法
strtok(string,split)
参数 | 描述 |
---|---|
string | 必需。规定要分割的字符串。 |
split | 必需。规定一个或多个分割字符。 |
<!DOCTYPE html> <html> <body> <?php $string = \"Hello world. Beautiful day today.\"; $token = strtok($string, \" \"); while ($token !== false) { echo \"$token<br>\"; $token = strtok(\" \"); } ?> </body> </html>
结果
Hello
world.
Beautiful
day
today.
3、使用substr()函数
定义和用法
substr()
函数返回字符串的一部分。
注释:如果 start 参数是负数且 length 小于或等于 start,则 length 为 0。
语法
substr(string,start,length)
参数 | 描述 |
---|---|
string | 必需。规定要返回其中一部分的字符串。 |
start | 必需。规定在字符串的何处开始。
正数 – 在字符串的指定位置开始 |
length | 可选。规定被返回字符串的长度。默认是直到字符串的结尾。
正数 – 从 start 参数所在的位置返回的长度 |
<!DOCTYPE html> <html> <body> <?php echo substr(\"Hello world\",6); ?> </body> </html>
结果
world
<!DOCTYPE html> <html> <body> <?php echo substr(\"Hello world\",10).\"<br>\"; echo substr(\"Hello world\",1).\"<br>\"; echo substr(\"Hello world\",3).\"<br>\"; echo substr(\"Hello world\",7).\"<br>\"; echo substr(\"Hello world\",-1).\"<br>\"; echo substr(\"Hello world\",-10).\"<br>\"; echo substr(\"Hello world\",-8).\"<br>\"; echo substr(\"Hello world\",-4).\"<br>\"; ?> </body> </html>
结果
d
ello world
lo world
orld
d
ello world
lo world
orld
<!DOCTYPE html> <html> <body> <?php echo substr(\"Hello world\",0,10).\"<br>\"; echo substr(\"Hello world\",1,8).\"<br>\"; echo substr(\"Hello world\",0,5).\"<br>\"; echo substr(\"Hello world\",6,6).\"<br>\"; echo substr(\"Hello world\",0,-1).\"<br>\"; echo substr(\"Hello world\",-10,-2).\"<br>\"; echo substr(\"Hello world\",0,-6).\"<br>\"; echo substr(\"Hello world\",-2-3).\"<br>\"; ?> </body> </html>
结果
Hello worl
ello wor
Hello
world
Hello worl
ello wor
Hello
world
三、字符串的比较
1、strcmp()比较两个字符串,如果相等,函数返回0
<!DOCTYPE html> <html> <body> <?php echo strcmp(\"Hello world!\",\"Hello world!\"); ?> </body> </html>
结果
0
2、strlen()函数测试字符串的长度
<!DOCTYPE html> <html> <body> <?php echo strlen(\"Shanghai\"); ?> </body> </html>
结果
8
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php常用函数与技巧总结》、《php字符串(string)用法总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
暂无评论内容