[Previous] [Contents] [Next]


Anchors

A regular expression can specify that a pattern occur at the start or end of a subject string using anchors. The ^ anchors a pattern to the start, and the $ character anchors a pattern to the end of a string. For example, the expression:

 ereg("^php", $var)

matches strings that start with "php" but not others. The following code shows the operation of both:

$var = "to be or not to be";

$match = ereg("^to", $var); // true
$match = ereg('be$', $var); // true
$match = ereg("^or", $var); // false

Both anchors can be used in one regular expression to match a whole string. The following example illustrates this:

// Must match "Yes" exactly
$match = ereg('^Yes$', "Yes");     // true
$match = ereg('^Yes$', "Yes sir"); // false

[Previous] [Contents] [Next]