PHP常見字符串操作函數(shù)與用法總結(jié)
本文實(shí)例講述了PHP常見字符串操作函數(shù)與用法。分享給大家供大家參考,具體如下:
一、字符串的格式化
1、字符串的格式化
trim()
函數(shù)可以去除字符串的開始位置和結(jié)束位置的空格,并將結(jié)果字符串返回,默認(rèn)情況下去除的字符是換行符和回車符(\n和\r),水平和垂直制表符(\t和X0B)
ltrim()
函數(shù)只從字符的開始處(左邊)去除空格
rtrim()
函數(shù)只從函數(shù)的結(jié)束處(右邊)去除空格
2、格式化字符串以便顯示
①使用HTML格式化:n12br()
函數(shù)
在字符串中的新行(\n)之前插入換行符
<?php echo nl2br("One line.\nAnother line."); ?>
結(jié)果
One line.
Another line.
②為打印輸出而格式化字符串
printf()
結(jié)構(gòu)
$s="world"); printf("Hello %s",$s);
3.改變字符串中的字母大小寫
函數(shù) | 描述 | 使用 $subject=Hello World | 返回值 |
---|---|---|---|
strtoupper() | 將字符串轉(zhuǎn)為大寫 | strtoupper($subject ) | HELLO WORLD |
strtolower() | 將字符串轉(zhuǎn)為小寫 | strtolower($subject ) | hello world |
ucfirst() | 如果字符串第一個字符是字符,將其轉(zhuǎn)為大寫 | ucfirst($subject ) | Hello world |
ucwords() | 將字符串的每個單詞的首字母大寫 | ucwords($subject ) | Hello World |
二、用字符串函數(shù)連接和分割字符串
1、用函數(shù)explode()、implode()和join()
exlpode()
把字符串打散為數(shù)組:
<!DOCTYPE html> <html> <body> <?php $str = "Hello world. I love Shanghai!"; print_r (explode(" ",$str)); ?> </body> </html>
結(jié)果
Array ( [0] => Hello [1] => world. [2] => I [3] => love [4] => Shanghai! )
implode()
(jion()
是implode()
函數(shù)的別名)
把數(shù)組元素組合為字符串:
<!DOCTYPE html> <html> <body> <?php $arr = array('Hello','World!','I','love','Shanghai!'); echo implode(" ",$arr); ?> </body> </html>
結(jié)果
Hello World! I love Shanghai!
2、使用strtok()函數(shù)
strtok()
函數(shù)把字符串分割為更小的字符串(標(biāo)記)。
語法
strtok(string,split)
參數(shù) | 描述 |
---|---|
string | 必需。規(guī)定要分割的字符串。 |
split | 必需。規(guī)定一個或多個分割字符。 |
<!DOCTYPE html> <html> <body> <?php $string = "Hello world. Beautiful day today."; $token = strtok($string, " "); while ($token !== false) { echo "$token<br>"; $token = strtok(" "); } ?> </body> </html>
結(jié)果
Hello
world.
Beautiful
day
today.
3、使用substr()函數(shù)
定義和用法
substr()
函數(shù)返回字符串的一部分。
注釋:如果 start 參數(shù)是負(fù)數(shù)且 length 小于或等于 start,則 length 為 0。
語法
substr(string,start,length)
參數(shù) | 描述 |
---|---|
string | 必需。規(guī)定要返回其中一部分的字符串。 |
start | 必需。規(guī)定在字符串的何處開始。
|
length | 可選。規(guī)定被返回字符串的長度。默認(rèn)是直到字符串的結(jié)尾。
|
<!DOCTYPE html> <html> <body> <?php echo substr("Hello world",6); ?> </body> </html>
結(jié)果
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>
結(jié)果
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>
結(jié)果
Hello worl
ello wor
Hello
world
Hello worl
ello wor
Hello
world
三、字符串的比較
1、strcmp()比較兩個字符串,如果相等,函數(shù)返回0
<!DOCTYPE html> <html> <body> <?php echo strcmp("Hello world!","Hello world!"); ?> </body> </html>
結(jié)果
0
2、strlen()函數(shù)測試字符串的長度
<!DOCTYPE html> <html> <body> <?php echo strlen("Shanghai"); ?> </body> </html>
結(jié)果
8
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php常用函數(shù)與技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP 刪除文件與文件夾操作 unlink()與rmdir()這兩個函數(shù)的使用
有時候我們需要用PHP來刪除文件和文件夾,PHP本來也都有函數(shù)可以實(shí)現(xiàn),下面簡單記錄一下代碼,方便以后信守拈來。2011-07-07php 實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換(二進(jìn)制、八進(jìn)制、十六進(jìn)制)互相轉(zhuǎn)換實(shí)現(xiàn)代碼
php 實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換(二進(jìn)制、八進(jìn)制、十六進(jìn)制)互相轉(zhuǎn)換實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-10-10PHP jpgraph庫的配置及生成統(tǒng)計(jì)圖表:折線圖、柱狀圖、餅狀圖
本篇文章主要介紹了PHP jpgraph庫的配置及生成統(tǒng)計(jì)圖表:折線圖、柱狀圖、餅狀圖等的相關(guān)知識。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-05-05php模仿asp Application對象在線人數(shù)統(tǒng)計(jì)實(shí)現(xiàn)方法
這篇文章主要介紹了php模仿asp Application對象在線人數(shù)統(tǒng)計(jì)實(shí)現(xiàn)方法,通過一個比較簡單的自定義函數(shù)實(shí)現(xiàn)這一功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01PHP實(shí)現(xiàn)ASCII碼與字符串相互轉(zhuǎn)換的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)ASCII碼與字符串相互轉(zhuǎn)換的方法,涉及php字符串的遍歷、替換、編碼轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2017-04-04SQL server不支持utf8 php卻用utf8的矛盾問題解決方法
這篇文章主要介紹了SQL server不支持utf8 php卻用utf8的矛盾問題解決方法,需要的朋友可以參考下2020-03-03php實(shí)現(xiàn)文件上傳及頭像預(yù)覽功能
這篇文章主要介紹了php實(shí)現(xiàn)文件上傳及頭像預(yù)覽功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01