微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php常用字符串String函数实例总结【转换,替换,计算,截取,加密】

本文实例总结了PHP常用字符串String函数分享给大家供大家参考,具体如下:

nl2br

功能:化换行符为

rush:PHP;"> PHP $str = "cat isn't \n dog"; $result = nl2br($str); echo $result; /**结果 cat isn't dog */

rtrim

功能:清除右边的空白

rush:PHP;"> PHP $str = "Hello world "; echo strlen($str)."
"; $result = rtrim($str); echo strlen($result); /**结果 14 11 */

strip_tags

功能:清除字符串中html和PHP标记

rush:PHP;"> PHP $str = "Hello world"; $result = strip_tags($str); echo $result; /**结果 Hello world */

strtolower 与 strtoupper

功能:转换成大小写

rush:PHP;"> PHP $str = "Hello World!"; $result = strtolower($str); echo $result."
"; $result = strtoupper($str); echo $result; /**结果 Hello World! Hello World! */

trim

功能去除首尾空格

rush:PHP;"> 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

功能:替换

rush:PHP;"> PHP $str = "zhang san"; $result = str_ireplace("zhang","li",$str); echo $str."
"; echo $result; /**结果 zhang san li san */

str_repeat

功能:将一个字符串重复多遍

rush:PHP;"> 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

功能:区分大小写的替换

rush:PHP;"> PHP $str = "hello jiqing!"; $result1 = str_ireplace("Hello","Hi",$str); //不区分大小写 $result2 = str_replace("Hello",$str); //区分大小写 echo $str."
"; echo $result1."
"; echo $result2."
"; /**结果 hello jiqing! Hi jiqing! hello jiqing! */

str_word_count

功能:返回字符串中单词的个数

rush:PHP;"> 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

功能:返回字符串长度

rush:PHP;"> PHP $str = "hello jiqing a!"; $result = strlen($str); echo $result; /**结果 15 */

substr_count

功能:计算一个字符串在另一个字符串中的个数

rush:PHP;"> PHP $str = "hello jiqing,hello jim!"; $result = substr_count($str,"hello"); echo $result; /**结果 2 */

substr_replace

功能:从某个位置开始替换

rush:PHP;"> PHP $str = "hello jiqing,hello jim!"; $result = substr_replace($str,"zhangsan",6); echo $result."
"; $result = substr_replace($str,6,6);//从某个位置替换,替换几个字符串 echo $result; /**结果 hello zhangsan hello zhangsan,hello jim! */

substr

功能获取子字符串

rush:PHP;"> PHP $str = "abcdef"; $result = substr($str,1); //从第0个开始,获取1个 echo $result."
"; $result = substr($str,-1);//从第0个开始,获取到除了最后一个的字符串 echo $result."
"; $result = substr($str,2,-1);//从第2个开始,获取到除了最后一个的字符串 echo $result."
"; $result = substr($str,-3,-1);//从第-3个开始,获取到除了最后一个的字符串 echo $result."
"; $result = substr($str,1);//从第-3个开始,获取到除了最后一个的字符串 echo $result."
"; /**结果 a abcde cde de d */

implode

功能:将数组转化为字符串

rush:PHP;"> PHP $array = array("2013","6","3"); $date = implode("/",$array); echo $date; /**结果 2013/6/3 */

md5

功能:对字符串进行md5加密

rush:PHP;"> PHP $str = "Hello world"; $result = md5($str); echo $result; /**结果 3e25960a79dbc69b674cd4ec67a72c62 */

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》及《

希望本文所述对大家PHP程序设计有所帮助。

原文地址:https://www.jb51.cc/php/18319.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐