background image

*将会输出: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";

?> 
 
2.preg_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]保存第一个子组的所有匹配,

以此类推。