klenwell information services : Paste20100418

PHP Ungreedy Search Example


Basic Format

DELIMITER START_FLAG (.*?) END_FLAG DELIMITER
$regex_p = '~#####(.*?)#####~';
$regex_p = sprintf('~%s(%s)%s~', preg_quote('<body>'), '.*?', preg_quote('</body>'));


Code Sample

$ungreedy_regex = sprintf('~%s~', '###(.*?)###');
$target = '(start)this is the # content ## we should extract(end)';
$subject = sprintf('###%s###', $target);

preg_match_all($ungreedy_regex, $subject, $Matches);

if ( $Matches[1][0] == $target ) {
    print '<h2 style="color:green;">success</h2>';
}
else {
    print '<h2 style="color:red;">fail</h2>';
}

$Data = array(
    'target' => $target,
    'preg_match_all' => $Matches
);

printf('<pre>%s</pre>', print_r($Data,1));
printf('<h4>code</h4>%s', highlight_file(__FILE__,1));


CategoryPastebin