自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP函數(shù)preg_split的正確使用方法

開發(fā) 后端
PHP函數(shù)preg_split的語法為array preg_split ( string $pattern, string $subject [, int $limit [, int $flags]] )。我們將舉例為大家詳細描述這個函數(shù)的用法。

對于初學者來說,大家對PHP函數(shù)preg_split的了解還不是很透徹,因為它的使用哪個方法有些復雜,比較難懂。今天我們就為大家?guī)砹怂木唧w介紹,希望能夠幫助讀者朋友加深對PHP函數(shù)preg_split的理解。

#t#說明 array preg_split ( string $pattern, string $subject [, int $limit [, int $flags]] )

返回一個數(shù)組,包含 subject 中沿著與 pattern 匹配的邊界所分割的子串。

如果指定了 limit,則最多返回 limit 個子串,如果 limit 是 -1,則意味著沒有限制,可以用來繼續(xù)指定可選參數(shù) flags。

flags 可以是下列標記的任意組合(用按位或運算符 | 組合):

PREG_SPLIT_NO_EMPTY
如果設定了本標記,則 preg_split() 只返回非空的成分。
PREG_SPLIT_DELIM_CAPTURE
如果設定了本標記,定界符模式中的括號表達式也會被捕獲并返回。本標記添加于 PHP 4.0.5。
PREG_SPLIT_OFFSET_CAPTURE
如果設定了本標記,如果設定本標記,對每個出現(xiàn)的匹配結(jié)果也同時返回其附屬的字符串偏移量。注意這改變了返回的數(shù)組的值,使其中的每個單元也是一個數(shù)組,其中第一項為匹配字符串,第二項為其在 subject 中的偏移量。本標記自 PHP 4.3.0 起可用。

提示

如果不需要正則表達式的功能,可以選擇使用更快(也更簡單)的替代函數(shù)如 explode() 或 str_split()。

例 1672. PHP函數(shù)preg_split例子:取得搜索字符串的成分

 

  1. // split the phrase by any number of commas or space characters,   
  2. // which include " ", \r, \t, \n and \f   
  3. $keywords = preg_split ("/[\s,]+/", "hypertext language, programming");   
  4. ?>  

 

例 1673.PHP函數(shù)preg_split 將字符串分割成字符

 

  1. $str = 'string';   
  2. $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);   
  3. print_r($chars);   
  4. ?>  

 

例 1674. PHP函數(shù)preg_split將字符串分割為匹配項及其偏移量

 

  1. $str = 'string';   
  2. $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);   
  3. print_r($chars);   
  4. ?>  

 

PHP函數(shù)preg_split例子將輸出:

 

  1. Array( [0] => 
  2. Array ( [0] => hypertext [1] => 0 ) [1] => 
  3. Array ( [0] => language [1] => 10 ) [2] => 
  4. Array ( [0] => programming [1] => 19 )

 

 

責任編輯:曹凱 來源: CSDN
相關推薦

2009-11-30 17:49:51

PHP函數(shù)preg_s

2009-11-30 17:43:54

PHP split()

2009-11-30 17:16:34

PHP函數(shù)preg_m

2009-12-02 14:50:25

PHP接口類inter

2009-12-07 16:52:59

PHP函數(shù)getima

2009-11-30 17:11:53

PHP函數(shù)preg_g

2009-11-26 19:05:04

PHP函數(shù)explod

2009-11-26 15:23:24

PHP函數(shù)ereg()

2009-11-16 16:54:00

PHP構(gòu)造函數(shù)

2010-10-08 14:27:25

JavascriptSplit

2009-12-01 19:02:20

PHP取整函數(shù)

2009-12-10 15:58:21

PHP preg_ma

2009-11-16 15:40:58

PHP數(shù)組函數(shù)

2011-07-12 17:18:23

PHPstrtotime

2009-12-02 16:04:44

PHP fsockop

2011-04-27 16:38:31

投影機

2010-03-04 15:17:30

Python prin

2009-11-24 16:18:14

PHP5析構(gòu)函數(shù)

2009-11-30 15:00:19

PHP加密解密函數(shù)au

2009-10-26 13:36:58

VB.NET Spli
點贊
收藏

51CTO技術(shù)棧公眾號