Aug 23, 2012

Note on preg_match_all of PHP

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


preg_match_all ( '/(<a[^>]+>).*(<\/a)/isU', $html, $matches)

$matches[0] : arrays that match whole regex  Ex:
$matches[0][0] => <a href="http://abc0.com">link0 </a>
$matches[0][1] => < a href="http://abc1.com">link1 </a>
$matches[0][2] => < a href="http://abc2.com">link2 </a>
..........


$matches[1] : arrays of matches group 1  ==> (<a[^>]+>)
$matches[1][0] => <a href="http://abc0.com">
$matches[1][1] => < a href="http://abc1.com">
$matches[1][2] => < a href="http://abc2.com">
..........



$matches[2] : arrays of matches group 1  ==> (<\/a>)
$matches[2][0] => </a>
$matches[2][1] => </a>
$matches[2][2] => </a>
..........



To get the link address....
preg_match_all('/(<a href=\")(.*)(\").*(<\/a>)/isU',$c->getHtml(),$matches)


Then, $matches[1]  will be ...
$matches[1][0] => http://abc0.com
$matches[1][1] => http://abc1.com
$matches[1][2] => http://abc2.com
..........

No comments:

Post a Comment