3. monica deshmane(H.V.Desai college)
3
1. Matching
$b = ereg(pattern, string [,captured]);
Depending on match it returns count for number of
matches and also populates the array.
echo "<br>".ereg('c(.*)e$','Welcome', $arr);
echo "<br>";
print_r($arr);
Output:
4
Array ( [0] => come [1] => om )
0th element is set to the entire string being matched and 1st element is substring that matched.
Regular Expression
1.POSIX
Uses of POSIX Functions
4. monica deshmane(H.V.Desai college) 4
$email_id = "admin@tutorialspoint.com";
$retval =preg_match("/.com$/", $email_id);
if( $retval == true )
{
echo "Found a .com<br>";
} else {
echo "Could not found a .com<br>";
}
Try this-
5. monica deshmane(H.V.Desai college) 5
<?php
$email = "abc123@sdsd.com";
$regex = '/^([a-z0-9])+@([a-z0-9])+.([a-z]{2,3})$/';
if (preg_match($regex, $email)) {
echo $email . " is a valid email. We can accept it.";
} else {
echo $email . " is an invalid email. Please try again.";
}
?>
Email validation
6. monica deshmane(H.V.Desai college)
6
2.Replacing
$str = ereg_replace(pattern, replacement, string)
The ereg_replace() function searches for string
specified by pattern and replaces pattern with
replacement if found.
Like ereg(), ereg_replace() is case sensitive.
eregi_replace is case insesnsitive.
It returns the modified string, but if no matches are
found, the string will remain unchanged.
e.g.
$copy_date = “june1999";
$copy_date1 = ereg_replace("([0-9]+)", "2000", $copy_date);
print $copy_date1;
Output: june2000
Regular Expression
1.POSIX
Uses of POSIX Functions
9. monica deshmane(H.V.Desai college)
9
1. Searching pattern at the start of string(^)
$b = ereg('^We', 'Welcome to PHP Program');
echo $b;
2. Searching pattern at the end of string($)
$b = ereg('We$', 'Welcome to PHP Program');
echo $b;
3. Dot (.)/period operator Used to match single character
echo "<br>".ereg('n.t', 'This is not a word'); //1
echo "<br>".ereg('n.t', 'nut is not here'); //1
echo "<br>".ereg('n.t', 'Take this bat'); //0
echo "<br>".ereg('n.t', 'n t'); //1
echo "<br>".ereg('n.t', 'nt'); //0
Regular Expression
1.POSIX Basics of Regular Expression
10. monica deshmane(H.V.Desai college)
10
3. Splitting
$arr = ereg_split(pattern, string[, limit])
The split() function will divide a string into various elements,
the boundaries of each element based on the occurrence of pattern in string.
The optional parameter limit is used to show the number of elements
into which the string should be divided,
starting from the left end of the string and working rightward.
In cases where the pattern is an alphabetical character, split() is case sensitive.
It returns an array of strings after splitting up a string.
Regular Expression
1.POSIX
Uses of POSIX Functions
11. monica deshmane(H.V.Desai college)
11
4. Matching special character /meta character use backslash or escape.
echo ereg(‘$50.00', 'Bill amount is $50.00'); //1
echo ereg(‘$50.00', 'Bill amount is $50.00'); //0
RE are case sensitive by default.
To perform case insensitive use eregi() used.
Abstract patterns-
1)Character class
2)Alternatives
3)Repeating sequences
Regular Expression
1.POSIX Basics of Regular Expression
12. monica deshmane(H.V.Desai college)
12
Character class is used to specify set of acceptable
characters in pattern.
defined by enclosing the acceptable characters in square bracket.
• Acceptable characters are-alphabets,numeric,punctuation marks.
• alphabets
echo "<br>".ereg('ch[aeiou]t', 'This is online chat'); //1
echo "<br>".ereg('ch[aeiou]t', 'See the chit'); //1
echo "<br>".ereg('ch[aeiou]t', ‘charitable'); //0
Regular Expression
Abstract patterns 1) Character classes
13. monica deshmane(H.V.Desai college)
13
•negate/except
Specify the ^ sign at the start of character class, this will negate/except the condition.
echo "<br>".ereg('ch[^aeiou]t', 'This is online chat'); //0
echo "<br>".ereg('ch[^aeiou]t', 'See the chit'); //0
echo "<br>".ereg('ch[^aeiou]t', 'chrt'); //1
• Use hypen(-) to specify the range of characters.
echo "<br>".ereg('[0-9]th', ‘This number is 10th'); //1
echo "<br>".ereg('[0123456789]th', This number is 7th'); //1
echo "<br>".ereg('[a-z]e', 'Welcome to internet Programming'); //1
echo "<br>".ereg('[a-z][A-Z]e', 'Welcome to internet Programming '); //1
Regular Expression
Abstract patterns
1) Character classes
14. monica deshmane(H.V.Desai college)
14
Character classes
[:alnum:] Alphanumeric characters [0-9a-zA-Z]
[:alpha:] Alphabetic characters [a-zA-Z]
[:blank:] space and tab [ t]
[:digit:] Digits [0-9]
[:lower:] Lower case letters [a-z]
[:upper:] Uppercase letters [A-Z]
[:xdigit:] Hexadecimal digit [0-9a-fA-F]
range of characters ex. [‘aeiou’]
Range ex. [2,6]
for special character Ex . [‘$’]
echo ereg('[[:digit:]]','23432434'); //1
echo ereg(‘[0-9]','23432434'); //1
Regular Expression
Abstract patterns 1) Character classes
15. monica deshmane(H.V.Desai college)
15
Vertical Pipe(|) symbol is used to specify alternatives.
echo "<br>".ereg('Hi|Hello', 'Welcome, Hello guys'); //1
echo "<br>".ereg('Hi|Hello', 'Welcome, Hi ameya'); //1
echo "<br>".ereg('Hi|Hello', 'Welcome, how are u?'); //0
String start with Hi or ends with Hello
echo "<br>".ereg('^Hi|Hello$', 'Hi, are u there?'); //1
echo "<br>".ereg('^Hi|Hello$', 'Hello, are u fine?'); //0
echo "<br>".ereg('^Hi|Hello$', 'how do u do?, Hello');//1
Regular Expression
Abstract patterns 2) Alternatives
16. monica deshmane(H.V.Desai college)
16
? 0 or 1
* 0 or more
+ 1 or more
{n} Exactly n times
{n,m} Atleast n, no more than m times
{n,} Atleast n times
echo "<br>".ereg('Hel+o', 'Hellllo'); //1
echo "<br>".ereg('Hel+o', 'Helo'); //0
echo "<br>".ereg('Hel?o', 'Helo'); //1
echo "<br>".ereg('Hel*o', 'Hellllo'); //1
echo "<br>".ereg('[0-9]{3}-[0-9]{3}-[0-9]{4}', '243-543-3567‘); //1
Regular Expression
Abstract patterns
3) Repeating Sequences
17. Subpatterns
echo "<br>".ereg('a (very )+good', 'Its a very very
good idea'); //1
Parentheses is used to group bits of regular
expression.
monica deshmane(H.V.Desai college) 17
18. preg_grep
preg_grep ( string $pattern , array $input [, int $flags = 0 ] ) :
Return array entries that match the pattern
Returns an array indexed using the keys from the input array.
$input = [ "Redish", "Pinkish", "Greenish","Purple"];
$result = preg_grep("/^p/i", $input);
print_r($result);
?>
monica deshmane(H.V.Desai college) 18