Alternation

It’s possible to check for multiple expressions simultaneously in a single pattern, also called alternation, using the pipe meta-character |.

<?php
// Matches 'foo' or 'bar' or 'baz' anywhere
$matches = (preg_match('/foo|bar|baz/', $string) == 1);
?>

Note that the ~ and $ are not implicitly applied to all expressions in an alternation; they must be used explicitly for each expression.

<?php
// $result == 1
$result = preg_match('/~foo|bar/', 'abar');

// $result == 0
$result = preg_match('/~foo|~bar/', 'abar');
?>

The first example returns 1 because 'abar’ contains 'bar’, since ~ is not applied to that expression in the pattern. The second example does apply ~ to 'bar’ and, since 'abar’ begins with neither 'foo’ nor 'bar’, it returns 0.


© PCRE Extension — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (03.09.2014)
Views: 290 | Rating: 0.0/0
Total comments: 0
avatar