PHP學習系列(1)——字串處理函數(2)

來源:互聯網
上載者:User

標籤:style   blog   color   使用   ar   sp   div   on   log   

6、chunk_split() 函數把字串分割為一連串更小的部分。本函數不改變原始字串。

文法:chunk_split(string,length,end)

參數:

string——必需。規定要分割的字串。

length——可選。一個數字,定義字串塊的長度。

end——可選。字串值,定義在每個字串塊之後放置的內容。

例子 1

本例分隔每個字元,並添加 ".":

<?php$str = "Hello world!";echo chunk_split($str,1,".");?>

輸出:

H.e.l.l.o. .w.o.r.l.d.!.
例子 2

本例將在六個字元之後分割一次字串,並添加 "...":

<?php$str = "Hello world!";echo chunk_split($str,6,"...");?>

輸出:

Hello ...world!...
7、convert_cyr_string() 函數把字元由一種 Cyrillic 字元轉換成另一種。

被支援的 Cyrillic 字元集是:

  • k - koi8-r
  • w - windows-1251
  • i - iso8859-5
  • a - x-cp866
  • d - x-cp866
  • m - x-mac-cyrillic
文法
convert_cyr_string(string,from,to)
8、convert_uudecode() 函數對 uuencode 編碼的字串進行解碼。
文法:convert_uudecode(string)例子

在本例中,我們將通過使用 convert_uudecode() 對 uuencode 編碼的字串進行解碼:

<?php$str = ",2&5L;&\@=V]R;&0A `";echo convert_uudecode($str);?>

輸出:

Hello world!
9、convert_uuencode() 函數使用 uuencode 演算法對字串進行編碼。
文法:convert_uuencode(string)

注意:

本函數把所有字串(包括二進位的)轉換為可列印的字串,確保其網路傳輸的安全。uuencode 的字串比原字串增大大約 35%。

例子

在本例中,我們將使用 convert_uuencode() 對字串進行編碼:

<?php$str = "Hello world!";echo convert_uuencode($str);?>

輸出:

,2&5L;&\@=V]R;&0A `

10、count_chars() 函數返回字串所用字元的資訊。

文法
count_chars(string,mode)

參數
描述

string
必需。規定要檢查的字串。

mode

可選。規定返回模式。預設是 0。有以下不同的返回模式:

  • 0 - 數組,ASCII 值為鍵名,出現的次數為索引值
  • 1 - 數組,ASCII 值為鍵名,出現的次數為索引值,只列出出現次數大於 0 的值
  • 2 - 數組,ASCII 值為鍵名,出現的次數為索引值,只列出出現次數等於 0 的值
  • 3 - 字串,帶有所有使用過的不同的字元
  • 4 - 字串,帶有所有未使用過的不同的字元
執行個體例子 1

在本例中,我們將使用 count_chars() 來檢查字串,返回模式設定為 1:

<?php$str = "Hello World!";print_r(count_chars($str,1));?>

輸出:

Array([32] => 1[33] => 1[72] => 1[87] => 1[100] => 1[101] => 1[108] => 3[111] => 2[114] => 1)
例子 2

在本例中,我們將使用 count_chars() 來檢查字串,返回模式設定為 3:

<?php$str = "Hello World!";echo count_chars($str,3);?>

輸出:

 !HWdelor

PHP學習系列(1)——字串處理函數(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.