本文实例讲述了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 必需。规定在字符串的何处开始。
正数 – 在字符串的指定位置开始负数 – 在从字符串结尾开始的指定位置开始0 – 在字符串中的第一个字符处开始
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
如何选择北京高端建站公司 建立网站的要点是什么电商怎么做,都需要哪些条件?一文看懂二级域名申请流程免费注册域名网站靠谱吗?域名注册费用多少钱?中文域名如何使用 中文域名的作用是什么如何买域名?网站为什么要备案?服务器建站助手功能是怎样的?建站助手是怎么搭建网站的?域名注册查询网有哪些?购买域名后还需要备案吗?