PHP字元處理函數str_split、implode、strpos、substr和strlen使用總結

來源:互聯網
上載者:User

 

經常需要使用PHP處理字元string,PHP內建的字元函數提供了非常強大的功能,基本上能完成大部分字元處理操作,如使用str_split 函數將一個字元轉換成一個數組、implode函數將一個數群組轉換成一個字元、strpos函數在一個字元中尋找另一個字元、substr函數擷取字元中的某幾個字元、strlen函數擷取字元長度等等。這些都是PHP最基礎的知識,便於日常工作使用,我將這個函數做了一些總結,僅供參考。

1,如何將一個字元轉換成一個數組
解決方案:str_split()函數
樣本如下:
$biuuu = 'www.biuuu.com';
print_r(str_split($biuuu))
輸出結果:
Array
(
[0] => b
[1] => i
[2] => u
[3] => u
[4] => u
)

2,如何將一個數群組轉換成一個字元
解決方案:implode()函數
樣本如下:
$biuuu = array('b','i','u','u','u');
print_r(implode('',$biuuu));
輸出結果
biiuu

3,如何在一個字元中尋找另一個字元
解決方案:strpos或strstr
樣本如下:
$biuuu = 'this is my website biuuu.com';
$search = 'biuuu';
print_r(strpos($biuuu,$search));
輸出結果:
19

4,如何擷取字元中的某幾個字元
解決方案:substr()函數
樣本如下:
$biuuu = 'this is my website biuuu.com';
print_r(substr($biuuu,19));
輸出結果:
biuuu.com

5,如何擷取字元長度
解決方案:strlen()函數
樣本如下:
$biuuu = 'this is my website biuuu.com';
print_r(strlen($biuuu));
輸出結果
28

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.