Regular Expressions in PHP - PHP

Regular expressions offer a powerful way to test strings for the presence of patterns. They use a language all their own to describe patterns, a language that consists mostly of symbols. PHP has several functions that use regular expressions.

boolean ereg(string pattern, string text, array matches)
The ereg function evaluates the pattern argument as a regular expression and attempts to find matches in the text argument. If the optional matches argument is supplied, each match will be added to the array. TRUE is returned if at least one match is made, FALSE otherwise.
The first element in the matches array, with an index of zero, will contain the match for the entire regular expression. Subsequent elements of matches will contain the matches for subexpressions. These are the expressions enclosed in parentheses in the example.

// show User Agent
print("User Agent: $HTTP_USER_AGENT<BR> ");
// try to parse User Agent
$HTTP_USER_AGENT, $matches))
print("Full match: $matches[0]<BR> ");
print("Browser: $matches[1]<BR> ");
print("Major Version: $matches[2]<BR> ");
print("Minor Version: $matches[3]<BR> ");
print("User Agent not recognized");

string ereg_replace(string pattern, string replacement, string
Use ereg_replace to replace substrings within the text argument. Each time the pattern matches a substring within the text argument, it is replaced with the replacement argument. The text argument is unchanged, but the altered version is returned. If the pattern contains subexpressions in parentheses, the replacement argument may contain a special code for specifying which subexpression to replace. The form is to use two backslashes followed by a single digit, zero through nine. Zero matches the entire expression; one through nine each match the first nine subexpressions, respectively. Subexpressions are numbered left to right, which accounts for nested subexpressions.


string ereg_replace(string pattern, string replacement, string

boolean eregi(string pattern, string text, array matches)
The eregi function operates identically to ereg with the exception that letters are matched with no regard for upper or lower case.

string eregi_replace(string pattern, string replacement, string text)
The eregi_replace function operates identically to ereg_replace with the exception that letters are matched with no regard for upper or lower case.array split(string pattern, string text, integer limit)

The split function returns an array of substrings from the text argument. The pattern argument will be used as a field delimiter. The optional limit argument sets the maximum number of elements to return. There is no case-insensitive version of split. Compare this function to explode, which uses a simple string to delimit substrings. Regular expression processing is slower than straight string matching, so use explode when you can.

$paragraph = "This is a short paragraph. Each ";
$paragraph .= "sentence will be extracted by ";
$paragraph .= "the split function. As a ";
$paragraph .= "result, you will be amazed!";
$sentence = split("[.!?]", $paragraph);
for($index = 0; $index < count($sentence);
print("$index. $sentence[$index] <BR> ");

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics