Mastering Regular Expressions in PHP
By Dennis Pallett2005-05-26
Another Example
<?phpThe regex is fairly simple, because we use d. This basically means "match any digit" with the length behind it. In this example it first looks for 3 digits, then a '-' (hyphen) and finally 7 digits. Works perfectly, and does exactly what we want.
// Good number
$good = "123-4567890";
// Bad number
$bad = "45-3423423";
// Let's check the good number
if (preg_match("/d{3}-d{7}/", $good)) {
echo "Valid number";
} else {
echo "Invalid number";
}
echo '<br />';
// And check the bad number
if (preg_match("/d{3}-d{7}/", $bad)) {
echo "Valid number";
} else {
echo "Invalid number";
}
?>
Tutorial pages:
|
|
|||||||||
You might also want to check these out:
|
Leave a Comment on "Mastering Regular Expressions in PHP"
You must be logged in to post a comment.
Link to This Tutorial Page!

