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。
示例:
[U]复制代码[/U] 代码如下:
, PHP [1]=>HP)
*/
$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 "
"."\n";
/*
*将会输出:Array([0]=>Array([0]=>, PHP [1]=>11) [1]=>Array([0]=>HP [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 "
"."\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 "
"."\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]保存第一个子组的所有匹配,以此类推。
PREG_SET_ORDER:
结果排序为$matches[0]包含第一次匹配得到的所有匹配(包含子组), $matches[1]是包含第二次匹配到的所有匹配(包含子组)的数组,以此类推。
PREG_OFFSET_CAPTURE:
如果这个标记被传递,每个发现的匹配返回时会增加它相对目标字符串的偏移量。 注意这会改变matches中的每一个匹配结果字符串元素,使其 成为一个第0个元素为 匹配结果字符串,第1个元素为 匹配结果字符串在subject中的偏移量。
返回值:
返回完整匹配次数(可能是0),或者如果发生错误返回FALSE。
示例:
[U]复制代码[/U] 代码如下:
, PHP [1]=>HP)
*/
$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 "
"."\n";
/*
*将会输出:Array([0]=>Array([0]=>, PHP [1]=>e php) [1]=>Array([0]=>HP [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 "
"."\n";
/*
*将会输出:Array([0]=>Array([0]=>Array([0]=>, PHP [1]=>11) [1]=>Array([0]=>HP [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 "
"."\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 "
"."\n";
?> 3.preg_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 后得到 的子串组成的数组。
示例:
[U]复制代码[/U] 代码如下:
In my point, [1] => is the web scripting language of choice. I love [2] => )
*/
$matches = array();
print_r(preg_split("/php/i", "In my point, PHP is the web scripting language of choice. I love php"));
echo "
"."\n";
/*
*将会输出:
*Array ( [0] => In my point, [1] => is the web scripting language of choice. I love php )
*/
$matches = array();
print_r(preg_split("/php/i", "In my point, PHP is the web scripting language of choice. I love php", 2));
echo "
"."\n";
/*
*将会输出:
*Array ( [0] => In my point, [1] => is the web scripting language of choice. I love )
*/
$matches = array();
print_r(preg_split("/php/i", "In my point, PHP is the web scripting language of choice. I love php", -1, PREG_SPLIT_NO_EMPTY));
echo "
"."\n";
?> 4.preg_quote — 转义正则表达式字符 string preg_quote ( string $str [, string $delimiter = NULL ] )
preg_quote()需要参数 str 并向其中 每个正则表达式语法中的字符前增加一个反斜线。 这通常用于你有一些运行时字符串 需要作为正则表达式进行匹配的时候。
正则表达式特殊字符有: . \ + * ? [ ^ ] $ ( ) { } = ! | : -
str:
输入字符串
delimiter:
如果指定了可选参数 delimiter,它也会被转义。这通常用于 转义PCRE函数使用的分隔符。 /是最通用的分隔符。
返回值:
返回转义后的字符串。
示例:
[U]复制代码[/U] 代码如下: