PHP » Strings » ereg()

Syntax:
int ereg(string pattern, string s [, array matches])
pattern
Pattern to match against.
s
The string.
matches
Array to return matches in.

Pattern matching with regular expressions.

This function has been deprecated as of PHP 5.3.0.

ereg() returns the first match of a regular expression pattern in a string. The optional "matches" parameter is an array in which the matches will be returned. The first element of the array is the portion of the string that the whole pattern matches. Subsequent elements are the submatches from the use of parentheses.

Examples

Code:
<pre>
<?php

$s = "This is not the sentence we are looking for."
   . " Coding PHP is fun."
   . " Here is another sentence we do not care about."
   . " More about PHP";

if (ereg("([a-zA-Z ]*)PHP([a-zA-Z ]*)", $s, $matches)) {
   print_r($matches);
} else {
   print "The pattern did not match anything";
}

?>
</pre>
Output:
Array
(
   [0] =>  Coding PHP is fun
   [1] =>  Coding
   [2] =>  is fun
)

Explanation:

A search for a sentence with the word "PHP" is performed. The result is printed, together with the parts of the sentences surrounding the word.

See Also: