在PHP中使用正則表達式進行查找替換

文章是對如何在PHP中使用正則表達式進行查找替換進行了詳細的分析介紹,需要的朋友參考下.

在PHP中使用正則表達式進行查找替換

  1. preg_match — 執行一個正則表達式匹配

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

搜索subject與pattern給定的正則表達式的一個匹配.

pattern:

要搜索的模式,字符串類型。

subject :

輸入字符串。

matches:

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

flags:

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

offset:

通常,搜索從目標字符串的開始位置開始。可選參數 offset 用於 指定從目標字符串的某個未知開始搜索(單位是字節)。

返回值:

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

示例:

複製代碼 代碼如下:

<?php

/*

*模式分隔符後的"i"標記這是一個大小寫不敏感的搜索

*將會輸出:1

*/

echo preg_match("/,s*(php)/i", "In my point, PHP is the web scripting language of choice.");

echo "<br/>"."n";

/*

*將會輸出:Array([0]=>, PHP [1]=>PHP)

*/

$matches = array();

preg_match("/,s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches);

print_r($matches);

echo "<br/>"."n";

/*

*將會輸出:Array([0]=>Array([0]=>, PHP [1]=>11) [1]=>Array([0]=>PHP [1]=>13))

*/

preg_match("/,s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches, PREG_OFFSET_CAPTURE);

print_r($matches);

echo "<br/>"."n";

/*

*將會輸出:Array([0]=>Array([0]=>e php [1]=63) [1]=>Array([0]=>php [1]=>65))

*/

preg_match("/[,a-z]?s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches, PREG_OFFSET_CAPTURE, 28);

print_r($matches);

echo "<br/>"."n";

?>

_match_all — 執行一個全局正則表達式匹配

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

搜索subject中所有匹配pattern給定正則表達式 的匹配結果並且將它們以flag指定順序輸出到matches中. 在第一個匹配找到後, 子序列繼續從最後一次匹配位置搜索.

pattern:

要搜索的模式,字符串形式。

subject :

輸入字符串。

matches:

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

flags:

可以結合下面標記使用(注意不能同時使用PREG_PATTERN_ORDER和PREG_SET_ORDER),如果沒有給定排序標記,假定設置爲PREG_PATTERN_ORDER:

PREG_PATTERN_ORDER:

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

PREG_SET_ORDER:

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

PREG_OFFSET_CAPTURE:

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

返回值:

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

示例:

複製代碼 代碼如下:

<?php

/*

*將會輸出:2

*/

echo preg_match_all("/php/i", "In my point, PHP is the web scripting language of choice. I love php", $matches);

echo "<br/>"."n";

/*

*將會輸出:Array([0]=>, PHP [1]=>PHP)

*/

$matches = array();

preg_match("/[,a-z]?s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches);

print_r($matches);

echo "<br/>"."n";

/*

*將會輸出:Array([0]=>Array([0]=>, PHP [1]=>e php) [1]=>Array([0]=>PHP [1]=>php))

*/

$matches = array();

preg_match_all("/[,a-z]?s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches, PREG_PATTERN_ORDER);

print_r($matches);

echo "<br/>"."n";

/*

*將會輸出:Array([0]=>Array([0]=>Array([0]=>, PHP [1]=>11) [1]=>Array([0]=>PHP [1]=>13)) [1]=>Array([0]=>Array([0]=>e php [1]=>63) [1]=>Array([0]=>php [1]=>65)))

*/

$matches = array();

preg_match_all("/[,a-z]?s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE);

print_r($matches);

echo "<br/>"."n";

/*

*Array([0]=>Array([0]=>e php [1]=>63) [1]=>Array([0]=>php [1]=>65))

*/

$matches = array();

preg_match_all("/[,a-z]?s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE, 28);

print_r($matches);

echo "<br/>"."n";

?>

_split — 通過一個正則表達式分隔字符串

array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )

通過一個正則表達式分隔給定字符串.

pattern:

用於搜索的模式,字符串形式。

subject:

輸入字符串

limit:

如果指定,將限制分隔得到的子串最多隻有limit個,返回的最後一個 子串將包含所有剩餘部分。limit值爲-1, 0或null時都代表"不限制", 作爲php的標準,你可以使用null跳過對flags的設置。

flags:

flags 可以是任何下面標記的組合(以位或運算 | 組合):

PREG_SPLIT_NO_EMPTY:

如果這個標記被設置, preg_split() 將進返回分隔後的非空部分。

PREG_SPLIT_DELIM_CAPTURE:

如果這個標記設置了,用於分隔的.模式中的括號表達式將被捕獲並返回。

PREG_SPLIT_OFFSET_CAPTURE:

如果這個標記被設置, 對於每一個出現的匹配返回時將會附加字符串偏移量. 注意:這將會改變返回數組中的每一個元素, 使其每個元素成爲一個由第0個元素爲分隔後的子串,第1個元素爲該子串在subject中的偏移量組成的數組。

返回值:

返回一個使用 pattern 邊界分隔 subject 後得到 的子串組成的數組。

示例:

複製代碼 代碼如下:

<?php

/*

*將會輸出:

*Array ( [0] => In my point, [1] => is the web scripting language of choice. I love [2] => )

*/

$matches = array();