[Previous] [Contents] [Next]


Alternative patterns


Alternatives in a pattern are specified with the | operator; for example, the pattern "cat|bat|rat" matches "cat", "bat", or "rat". The | operator has the lowest precedence of the regular expression operators, treating the largest surrounding expressions as alternative patterns. To match "cat", "bat", or "rat" another way, the following expression can be used:

$var = "bat";
$found = ereg("(c|b|r)at", $var);  // true

Another example shows alternative beginnings to a pattern:

// match some URLs
$pattern = '(^ftp|^http|^gopher)://';

$found = ereg($pattern, "http://www.ora.com"); // true

[Previous] [Contents] [Next]