本文實例總結了php常用字串String函數。分享給大家參考,具體如下:
nl2br
功能:化換行符為
<?php $str = "cat isn't \n dog"; $result = nl2br($str); echo $result; /**结果 cat isn't dog */
rtrim
功能:清除右邊的空白
<?php $str = "Hello world "; echo strlen($str)."<br>"; $result = rtrim($str); echo strlen($result); /**结果 14 11 */
strip_smm
功能:清除右邊的空白<?php $str = "<font color = 'red'>Hello world</font>"; $result = strip_tags($str); echo $result; /**结果 Hello world */strip_ss
字符串中和標籤php的標記
<?php $str = "Hello World!"; $result = strtolower($str); echo $result."<br>"; $result = strtoupper($str); echo $result; /**结果 hello world! HELLO WORLD! */
strtolower 與strtoupper
函數:轉換成大小寫
<?php $str = " Hello World! "; $result = trim($str); echo $str."<br>"; echo $result."<br>"; echo strlen($str)."<br>"; echo strlen($result); /**结果 Hello World! Hello World! 16 12 */
trim
功能:去除首尾空格
<?php $str = "zhang san"; $result = str_ireplace("zhang","li",$str); echo $str."<br>"; echo $result; /**结果 zhang san li san */
str_irerepplace
函數:去除首尾空格多遍
<?php $str = "Hello jiqing!"; $result = str_repeat($str,4); echo $str."<br>"; 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."<br>"; echo $result1."<br>"; echo $result2."<br>"; /**结果 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."<br>"; echo $result1."<br>"; print_r($result2); /**结果 hello jiqing a! 3 Array ( [0] => hello [1] => jiqing [2] => a ) */
str :計算一個字串在另一個字串中的個數
<?php $str = "hello jiqing a!"; $result = strlen($str); echo $result; /**结果 15 */
substr_replace
功能:從某個位置開始替換
<?php $str = "hello jiqing ,hello jim!"; $result = substr_count($str,"hello"); echo $result; /**结果 2 */
substr
功能:獲取子字串
<?php $str = "hello jiqing ,hello jim!"; $result = substr_replace($str,"zhangsan",6); echo $result."<br>"; $result = substr_replace($str,"zhangsan",6,6);//从某个位置替换,替换几个字符串 echo $result; /**结果 hello zhangsan hello zhangsan ,hello jim! */
implode
功能:獲取子字串
<?php $str = "abcdef"; $result = substr($str,0,1); //从第0个开始,获取1个 echo $result."<br>"; $result = substr($str,0,-1);//从第0个开始,获取到除了最后一个的字符串 echo $result."<br>"; $result = substr($str,2,-1);//从第2个开始,获取到除了最后一个的字符串 echo $result."<br>"; $result = substr($str,-3,-1);//从第-3个开始,获取到除了最后一个的字符串 echo $result."<br>"; $result = substr($str,-3,1);//从第-3个开始,获取到除了最后一个的字符串 echo $result."<br>"; /**结果 a abcde cde de d */
implode轉換為字串
<?php $array = array("2016","6","3"); $date = implode("/",$array); echo $date; /**结果 2016/6/3 */
md5
函數:對字串進行md5加密
<?php $str = "Hello world"; $result = md5($str); echo $result; /**结果 3e25960a79dbc69b674cd4ec67a72c62 */
以上就是(實用篇)php常用字串函數實例總結【轉換,替換,計算,截取,加密】的內容,更多相關內容請關注PHP中文網(www.php.cn)!