Monday, 12 August 2013

PHP Regular Expression with subexpression

PHP Regular Expression with subexpression

I came across the following code that has a regular expression which I do
not understand.
Specifically:
'#<!-- START '. $tag . ' -->(.+?)<!-- END '. $tag . ' -->#si'
PHP says that the function 'preg_match' will return the following:
" If matches is provided, then it is filled with the results of search.
$matches[0] will contain the text that matched the full pattern,
$matches[1] will have the text that matched the first captured
parenthesized subpattern, and so on. "
I take it that
(.+?)
is such a 'parenthesized subpattern.' Where can I read about subpatterns?
An official place that is not like w3schools.com.
/**
* Gets a chunk of page content
* @param String the tag wrapping the block ( <!-- START tag --> block
<!-- END tag --> )
* @return String the block of content
*/
public function getBlock( $tag )
{
preg_match ('#<!-- START '. $tag . ' -->(.+?)<!-- END '. $tag . '
-->#si', $this->content, $tor);
$tor = str_replace ('<!-- START '. $tag . ' -->', "", $tor[0]);
$tor = str_replace ('<!-- END ' . $tag . ' -->', "", $tor);
return $tor;
}
Any explanations along with links would be helpful!
Thanks!

No comments:

Post a Comment