Performing a Case-Sensitive Search
The ereg() function executes a case-sensitive search of a string for a defined pattern,
returning TRUE if the pattern is found, and FALSE otherwise. Its prototype follows:
boolean ereg(string pattern, string string [, array regs])
Here??™s how you could use ereg() to ensure that a username consists solely of
lowercase letters:
$username = "jasoN";
if (ereg("([^a-z])",$username))
echo "Username must be all lowercase!";
236 CHAPTER 9 ?– STRINGS AND REGULAR EXPRESSIONS
else
echo "Username is all lowercase!";
?>
In this case, ereg() will return TRUE, causing the error message to output.
The optional input parameter regs contains an array of all matched expressions that
are grouped by parentheses in the regular expression. Making use of this array, you
could segment a URL into several pieces, as shown here:
$url = "http://www.apress.com";
// Break $url down into three distinct pieces:
// "http://www", "apress", and "com"
$parts = ereg("^(http://www)\.([[:alnum:]]+)\.([[:alnum:]]+)", $url, $regs);
echo $regs[0]; // outputs the entire string "http://www.apress.com"
echo "
";
echo $regs[1]; // outputs "http://www"
echo "
";
echo $regs[2]; // outputs "apress"
echo "
";
echo $regs[3]; // outputs "com"
?>
This returns the following:
http://www.apress.com
http://www
apress
com
Performing a Case-Insensitive Search
The eregi() function searches a string for a defined pattern in a case-insensitive
fashion.
Pages:
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314