php中的preg系列函數

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   使用   sp   strong   

  • mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

如果subject是數組則返回數組,否則返回一個字串。如果發生錯誤,返回 NULL 。修飾符\e已經廢棄,請使用preg_replace_callback

$pattern,$replacement,$subject 都可以是數組也是字串

$replacement 表示後向引用的時候用\\n或者$n。在$replacement中使用反斜線,必須使用4個("\\\\",譯註:因為這首先是php的字串,經過轉義後,是兩個,再經過 Regex引擎後才被認為是一個原文反斜線)

一個後向引用後面跟另外一個數字需要使用 ${n}m

$limit表示每個模式在每個subject上進行替換的最大次數
$count 替換的次數

 

 

  • mixed preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]] )

這個函數的行為除了 可以指定一個 callback 替代 replacement 進行替換 字串的計算,其他方面等同於 preg_replace()。

 

<?php// 將文本中的年份增加一年.$text = "April fools day is 04/01/2002\n";$text.= "Last christmas was 12/24/2001\n";// 回呼函數function next_year($matches){  // 通常: $matches[0]是完成的匹配  // $matches[1]是第一個捕獲子組的匹配  // 以此類推  return $matches[1].($matches[2]+1);}echo preg_replace_callback(            "|(\d{2}/\d{2}/)(\d{4})|",            "next_year",            $text);?>April fools day is 04/01/2003Last christmas was 12/24/2002

 

  • int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

搜尋subject與pattern給定的Regex的一個匹配.

preg_match()返回 pattern 的匹配次數。 它的值將是0次(不匹配)或1次,因為preg_match()在第一次匹配後 將會停止搜尋。preg_match_all()不同於此,它會一直搜尋subject 直到到達結尾。如果發生錯誤preg_match()返回 FALSE

$matches
如果提供了參數matches,它將被填充為搜尋結果。 $matches[0]將包含完整模式比對到的文本, $matches[1] 將包含第一個捕獲子組匹配到的文本,以此類推。

$flags
flags可以被設定為以下標記值:

PREG_OFFSET_CAPTURE
如果傳遞了這個標記,對於每一個出現的匹配返回時會附加字串位移量(相對於目標字串的)。 注意:這會改變填充到matches參數的數組,使其每個元素成為一個由 第0個元素是匹配到的字串,第1個元素是該匹配字串 在目標字串subject中的位移量。

$offset
通常,搜尋從目標字串的開始位置開始。選擇性參數 offset 用於 指定從目標字串的某個位置開始搜尋(單位是位元組)。

<?php$subject = "abcdef";$pattern = ‘/^def/‘;preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);print_r($matches);?>Array(    [0] => Array        (            [0] => def            [1] => 0        ))

 

  • int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )

返回完整匹配次數(可能是0),或者如果發生錯誤返回FALSE。

matches
多維陣列,作為輸出參數輸出所有匹配結果, 數組排序通過flags指定。

flags

PREG_PATTERN_ORDER:結果排序為$matches[0]儲存完整模式的所有匹配, $matches[1] 儲存第一個子組的所有匹配,以此類推。

<?phppreg_match_all("|<[^>]+>(.*)</[^>]+>|U",    "<b>example: </b><div align=left>this is a test</div>",    $out, PREG_PATTERN_ORDER);echo $out[0][0] . ", " . $out[0][1] . "\n";echo $out[1][0] . ", " . $out[1][1] . "\n";?><b>example: </b>, <div align=left>this is a test</div>example: , this is a test

PREG_SET_ORDER

結果排序為$matches[0]包含第一次匹配得到的所有匹配(包含子組), $matches[1]是包含第二次匹配到的所有匹配(包含子組)的數組,以此類推。

<?phppreg_match_all("|<[^>]+>(.*)</[^>]+>|U",    "<b>example: </b><div align=\"left\">this is a test</div>",    $out, PREG_SET_ORDER);echo $out[0][0] . ", " . $out[0][1] . "\n";echo $out[1][0] . ", " . $out[1][1] . "\n";?><b>example: </b>, example:<div align="left">this is a test</div>, this is a test

PREG_OFFSET_CAPTURE

如果這個標記被傳遞,每個發現的匹配返回時會增加它相對目標字串的位移量。 注意這會改變matches中的每一個匹配結果字串元素,使其 成為一個第0個元素為匹配結果字串,第1個元素為 匹配結果字串在subject中的位移量。

 

  • array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )

返回給定數組input中與模式pattern 匹配的元素組成的數組.

flags

如果設定為PREG_GREP_INVERT, 這個函數返回輸入數組中與 給定模式pattern匹配的元素組成的數組.

$array = array(10.2,43,‘ab‘,3.4,5.5);$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array,1);print_r($fl_array);Array ( [1] => 43 [2] => ab )

 

  • string preg_quote ( string $str [, string $delimiter = NULL ] )

轉義Regex字元

preg_quote()需要參數 str 並向其中 每個Regex文法中的字元前增加一個反斜線。 這通常用於你有一些運行時字串 需要作為Regex進行匹配的時候。

Regex特殊字元有: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

$textbody = "This book is *very* difficult to find.";$word = "*very*";$textbody = preg_replace ("/" . preg_quote($word) . "/",                          "<i>" . $word . "</i>",                          $textbody);echo $textbody;This book is *very* difficult to find.

 

php中的preg系列函數

聯繫我們

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