PHP常用字符串函数概览
本示例详细归纳了PHP语言中高频使用的字符串(String)相关函数,旨在为开发者提供实用的参考指南。
nl2br
功能描述:将字符串中的换行符转换为HTML的<br />标签。
<?php
$str = "cat isn't \n dog";
$result = nl2br($str);
echo $result;
/**结果
cat isn't
dog
*/
rtrim
功能描述:移除字符串右侧末尾的空格或其他指定字符。
<?php
$str = "Hello world ";
echo strlen($str)."
";
$result = rtrim($str);
echo strlen($result);
/**结果
14
11
*/
strip_tags
功能描述:从字符串中剔除所有的HTML和PHP标记,仅保留纯文本内容。
<?php
$str = "Hello world";
$result = strip_tags($str);
echo $result;
/**结果
Hello world
*/
strtolower 与 strtoupper
功能描述:前者用于将字符串全部转换为小写,后者用于将字符串全部转换为大写。
<?php
$str = "Hello World!";
$result = strtolower($str);
echo $result."
";
$result = strtoupper($str);
echo $result;
/**结果
hello world!
HELLO WORLD!
*/
trim
功能描述:去除字符串头部和尾部多余的空格或特定字符。
<?php
$str = " Hello World! ";
$result = trim($str);
echo $str."
";
echo $result."
";
echo strlen($str)."
";
echo strlen($result);
/**结果
Hello World!
Hello World!
16
12
*/
str_ireplace
功能描述:执行不区分大小写的字符串替换操作。
<?php
$str = "zhang san";
$result = str_ireplace("zhang","li",$str);
echo $str."
";
echo $result;
/**结果
zhang san
li san
*/
str_repeat
功能描述:将指定的字符串重复拼接多次,生成新的长字符串。
<?php
$str = "Hello jiqing!";
$result = str_repeat($str,4);
echo $str."
";
echo $result;
/**结果
Hello jiqing!
Hello jiqing!Hello jiqing!Hello jiqing!Hello jiqing!
*/
str_replace
功能描述:执行严格区分大小写的字符串替换操作。
<?php
$str = "hello jiqing!";
$result1 = str_ireplace("Hello","Hi",$str); //不区分大小写
$result2 = str_replace("Hello","Hi",$str); //区分大小写
echo $str."
";
echo $result1."
";
echo $result2."
";
/**结果
hello jiqing!
Hi jiqing!
hello jiqing!
*/
str_word_count
功能描述:统计并返回字符串中包含的单词数量。
<?php
$str = "hello jiqing a!";
$result1 = str_word_count($str); //返回个数
$result2 = str_word_count($str,1); //返回数组
echo $str."
";
echo $result1."
";
print_r($result2);
/**结果
hello jiqing a!
3
Array ( [0] => hello [1] => jiqing [2] => a )
*/
strlen
功能描述:返回字符串的总字节长度。
<?php
$str = "hello jiqing a!";
$result = strlen($str);
echo $result;
/**结果
15
*/
substr_count
功能描述:计算一个子字符串在目标字符串中出现的次数。
<?php
$str = "hello jiqing ,hello jim!";
$result = substr_count($str,"hello");
echo $result;
/**结果
2
*/
substr_replace
功能描述:从字符串的指定位置开始,替换指定长度的内容。
<?php
$str = "hello jiqing ,hello jim!";
$result = substr_replace($str,"zhangsan",6);
echo $result."
";
$result = substr_replace($str,"zhangsan",6,6);//从某个位置替换,替换几个字符串
echo $result;
/**结果
hello zhangsan
hello zhangsan ,hello jim!
*/
substr
功能描述:从字符串中提取并返回指定起始位置和长度的子字符串片段。
<?php
$str = "abcdef";
$result = substr($str,0,1); //从第0个开始,获取1个
echo $result."
";
$result = substr($str,0,-1);//从第0个开始,获取到除了最后一个的字符串
echo $result."
";
$result = substr($str,2,-1);//从第2个开始,获取到除了最后一个的字符串
echo $result."
";
$result = substr($str,-3,-1);//从第-3个开始,获取到除了最后一个的字符串
echo $result."
";
$result = substr($str,-3,1);//从第-3个开始,获取到除了最后一个的字符串
echo $result."
";
/**结果
a
abcde
cde
de
d
*/
implode
功能描述:使用分隔符将数组元素连接合并为一个完整的字符串。
<?php
$array = array("2013","6","3");
$date = implode("/",$array);
echo $date;
/**结果
2013/6/3
*/
md5
功能描述:对输入的字符串进行MD5哈希运算,生成唯一的加密摘要。
<?php
$str = "Hello world";
$result = md5($str);
echo $result;
/**结果
3e25960a79dbc69b674cd4ec67a72c62
*/
希望上述关于PHP字符串函数的总结与分析,能对各位读者的编程实践有所帮助。