php常用字符串String函數(shù)實(shí)例總結(jié)【轉(zhuǎn)換,替換,計(jì)算,截取,加密】
本文實(shí)例總結(jié)了php常用字符串String函數(shù)。分享給大家供大家參考,具體如下:
nl2br
功能:化換行符為<br>
<?php $str = "cat isn't \n dog"; $result = nl2br($str); echo $result; /**結(jié)果 cat isn't dog */
rtrim
功能:清除右邊的空白
<?php $str = "Hello world "; echo strlen($str)."<br>"; $result = rtrim($str); echo strlen($result); /**結(jié)果 14 11 */
strip_tags
功能:清除字符串中html和php的標(biāo)記
<?php $str = "<font color = 'red'>Hello world</font>"; $result = strip_tags($str); echo $result; /**結(jié)果 Hello world */
strtolower 與 strtoupper
功能:轉(zhuǎn)換成大小寫
<?php $str = "Hello World!"; $result = strtolower($str); echo $result."<br>"; $result = strtoupper($str); echo $result; /**結(jié)果 hello world! HELLO WORLD! */
trim
功能:去除首尾空格
<?php $str = " Hello World! "; $result = trim($str); echo $str."<br>"; echo $result."<br>"; echo strlen($str)."<br>"; echo strlen($result); /**結(jié)果 Hello World! Hello World! 16 12 */
str_ireplace
功能:替換
<?php
$str = "zhang san";
$result = str_ireplace("zhang","li",$str);
echo $str."<br>";
echo $result;
/**結(jié)果
zhang san
li san
*/
str_repeat
功能:將一個(gè)字符串重復(fù)多遍
<?php $str = "Hello jiqing!"; $result = str_repeat($str,4); echo $str."<br>"; echo $result; /**結(jié)果 Hello jiqing! Hello jiqing!Hello jiqing!Hello jiqing!Hello jiqing! */
str_replace
功能:區(qū)分大小寫的替換
<?php
$str = "hello jiqing!";
$result1 = str_ireplace("Hello","Hi",$str); //不區(qū)分大小寫
$result2 = str_replace("Hello","Hi",$str); //區(qū)分大小寫
echo $str."<br>";
echo $result1."<br>";
echo $result2."<br>";
/**結(jié)果
hello jiqing!
Hi jiqing!
hello jiqing!
*/
str_word_count
功能:返回字符串中單詞的個(gè)數(shù)
<?php $str = "hello jiqing a!"; $result1 = str_word_count($str); //返回個(gè)數(shù) $result2 = str_word_count($str,1); //返回?cái)?shù)組 echo $str."<br>"; echo $result1."<br>"; print_r($result2); /**結(jié)果 hello jiqing a! 3 Array ( [0] => hello [1] => jiqing [2] => a ) */
strlen
功能:返回字符串長(zhǎng)度
<?php $str = "hello jiqing a!"; $result = strlen($str); echo $result; /**結(jié)果 15 */
substr_count
功能:計(jì)算一個(gè)字符串在另一個(gè)字符串中的個(gè)數(shù)
<?php $str = "hello jiqing ,hello jim!"; $result = substr_count($str,"hello"); echo $result; /**結(jié)果 2 */
substr_replace
功能:從某個(gè)位置開始替換
<?php $str = "hello jiqing ,hello jim!"; $result = substr_replace($str,"zhangsan",6); echo $result."<br>"; $result = substr_replace($str,"zhangsan",6,6);//從某個(gè)位置替換,替換幾個(gè)字符串 echo $result; /**結(jié)果 hello zhangsan hello zhangsan ,hello jim! */
substr
功能:獲取子字符串
<?php $str = "abcdef"; $result = substr($str,0,1); //從第0個(gè)開始,獲取1個(gè) echo $result."<br>"; $result = substr($str,0,-1);//從第0個(gè)開始,獲取到除了最后一個(gè)的字符串 echo $result."<br>"; $result = substr($str,2,-1);//從第2個(gè)開始,獲取到除了最后一個(gè)的字符串 echo $result."<br>"; $result = substr($str,-3,-1);//從第-3個(gè)開始,獲取到除了最后一個(gè)的字符串 echo $result."<br>"; $result = substr($str,-3,1);//從第-3個(gè)開始,獲取到除了最后一個(gè)的字符串 echo $result."<br>"; /**結(jié)果 a abcde cde de d */
implode
功能:將數(shù)組轉(zhuǎn)化為字符串
<?php
$array = array("2013","6","3");
$date = implode("/",$array);
echo $date;
/**結(jié)果
2013/6/3
*/
md5
功能:對(duì)字符串進(jìn)行md5加密
<?php $str = "Hello world"; $result = md5($str); echo $result; /**結(jié)果 3e25960a79dbc69b674cd4ec67a72c62 */
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- 總結(jié)PHP中數(shù)值計(jì)算的注意事項(xiàng)
- PHP中浮點(diǎn)數(shù)計(jì)算比較及取整不準(zhǔn)確的解決方法
- php計(jì)算函數(shù)執(zhí)行時(shí)間的方法
- PHP幾個(gè)數(shù)學(xué)計(jì)算的內(nèi)部函數(shù)學(xué)習(xí)整理
- php計(jì)算兩個(gè)整數(shù)的最大公約數(shù)常用算法小結(jié)
- PHP計(jì)算加權(quán)平均數(shù)的方法
- php數(shù)字游戲 計(jì)算24算法
- PHP之浮點(diǎn)數(shù)計(jì)算比較以及取整數(shù)不準(zhǔn)確的解決辦法
- PHP數(shù)據(jù)分析引擎計(jì)算余弦相似度算法示例
- php數(shù)值計(jì)算num類簡(jiǎn)單操作示例
相關(guān)文章
PHP帶節(jié)點(diǎn)操作的無限分類實(shí)現(xiàn)方法詳解
這篇文章主要介紹了PHP帶節(jié)點(diǎn)操作的無限分類實(shí)現(xiàn)方法,可實(shí)現(xiàn)無限分類及針對(duì)節(jié)點(diǎn)的添加、刪除、移動(dòng)等功能,需要的朋友可以參考下2016-11-11
php簡(jiǎn)單處理XML數(shù)據(jù)的方法示例
這篇文章主要介紹了php簡(jiǎn)單處理XML數(shù)據(jù)的方法,結(jié)合具體實(shí)例形式分析了php對(duì)xml格式數(shù)據(jù)的簡(jiǎn)單載入、讀取、輸出等操作技巧,需要的朋友可以參考下2017-05-05
Zend studio for eclipse中使php可以調(diào)用mysql相關(guān)函數(shù)的設(shè)置方法
默認(rèn)情況zend studio 中的php是不支持mysql 相關(guān)操作,但通過下面的方法即可解決2008-10-10
php 記錄進(jìn)行累加并顯示總時(shí)長(zhǎng)為秒的結(jié)果
用php如何將這些記錄進(jìn)行累加,最后顯示為一個(gè)總時(shí)長(zhǎng)為秒鐘的結(jié)果2011-11-11
基于PHP實(shí)現(xiàn)假裝商品限時(shí)搶購繁忙的效果
這篇文章主要介紹了基于PHP實(shí)現(xiàn)假裝商品限時(shí)搶購繁忙的效果,需要的朋友可以參考下2015-10-10
php使用PDO操作MySQL數(shù)據(jù)庫實(shí)例
這篇文章主要介紹了php使用PDO操作MySQL數(shù)據(jù)庫,實(shí)例分析了PDO的開啟與針對(duì)MySQL數(shù)據(jù)庫的增刪改查等基本操作方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
使用ThinkPHP自帶的Http類下載遠(yuǎn)程圖片到本地的實(shí)現(xiàn)代碼
Thinkphp是國(guó)人開發(fā)一個(gè)PHP框架,該框架相比國(guó)外的一些框架也毫不遜色。強(qiáng)大的ORM,插件,分組等功能讓人愛不釋手。2011-08-08

