星期三,2008年12月24日 21:54

PHP function : preg_match_all

<?php
 
/*

preg_match_all – Perform a global regular expression match.

int preg_match_all (string $pattern , string $subject , array &$matches [ , int $flags [ , int $offset]])
Search $subject for all matches to the regular expression given in $pattern and put them in $matches in the order specified by $flags.
After the first match is found , the subsequent searches are continue on from the end of the last match.

parameters:
$pattern : The pattern to search for , as a string.
$subject : The input string.
$matches : Array of all matches in muti-dimensional array ordered according to $flags.
$flags : Can be combination of following flags (note that it doesn’t make sense to use PREG_PATTERN_ORDER together with PREG_SET_ORDER):
                    PREG_PATTERN_ORDER: Orders result so that $matches[0] is an array of full pattern matches , $matches[1] is an array of strings matched by the first parenthesized subpattern , and so on.
                    If no order flag is given , PREG_PATTERN_ORDER is assummed.
                    PREG_SET_ORDER: Orders result so that $matches[0] is an array of first set of matches , $matches[1] is an array of second set of matches , and so on.
                    PREG_OFFSET_CAPTURE: If this flag is passed , for every occurring match the appendant string offset will also be returned. Note that this changes the value of $matches in an array where every element is an array consisting of the matched string at offset 0 and its string offset into $subject at offset 1.

$offset : Normally , the search start from the beginning of the $subject string. The optional parameter $offset can be used to specify the alternate place from which to start the search (in bytes).

return:
Return the number of full pattern matches (which might be zero) , or FALSE if an erro occrred.

*/ 

 

$str = ‘<b>yes</b> <strong>no</strong><br /> <a href=”http://kuigg.com“> kuigg </a>’;
$reg = ‘/<\s*(\w+)[^>]*>(.*)<\s*\/\s*\1\s*>/’;
preg_match_all ($reg , $str , $arr , PREG_SET_ORDER);
var_dump ($arr);
 
 echo ‘<br />
 ’;
 
preg_match_all ($reg , $str , $arr , PREG_PATTERN_ORDER);
var_dump ($arr);

 

?>

分类: php, 学习资料
您可以通过RSS 2.0订阅本文的评论。 您也可以发表评论,或者在您的网站引用本文