Web Design and Programming Pt 8 Regex

Regex TutorialIn this video I’ll continue to show you how to retrieve specific information using Regular Expressions and PHP. I’ll specifically cover how to find:

  • Email Addresses
  • Phone Numbers
  • Dates
  • Validated Passwords
  • And other more complicated Regular Expressions

You definitely should watch part 1 of this tutorial here PHP Regular Expressions, before you proceed. Regular expressions work nearly the same with all languages. The only main difference is the methods or functions that locate them. If you watch the videos you’ll better understand what I mean.

The first part of this whole tutorial is available here Web Design and Programming. If you have any questions or comments leave them below. Like always the code follows the video and you can use it in any way.

Code from the Video

<html>
<head>
<title><?php echo “Regular Expressions”;?></title>
</head>

<body>

<?php
$randomArray = array(“Derek”,”123 Main St.”,”PA”,”12345″,”(412)-537-5555″,
“12/12/1974″,”dbanas123@gmail.com”,”$1,234″,”Turtle3Dove”,”123-45-6789″,”p* 1 “,
“<p>Random Text</p>”, “Mailman”, “Mailwoman”, “Jennifer”, “Jenny”, “Jen”, “Doctor”, “Doug”, “Dog”);

$emailArray = preg_grep(“%[A-Za-z0-9._\%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}%”, $randomArray);

foreach($emailArray as $result)
{
echo $result, “<br /><br />”;
}

$phoneArray = preg_grep(“%\(?[0-9]{3}\)?-?[0-9]{3}[-. ]?[0-9]{4}%”, $randomArray);

foreach($phoneArray as $result)
{
echo $result, “<br /><br />”;
}

# ? – Occurs zero or one time
# / @ # ` ~ % & ‘ ” – All possible delimiters

$dateArray = preg_grep(“%(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}%”, $randomArray);

foreach($dateArray as $result)
{
echo $result, “<br /><br />”;
}

$passwordArray = preg_grep(‘%\A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])\S{6,}\z%’, $randomArray);

foreach($passwordArray as $result)
{
echo $result, “<br />”;
}

# (?=) – Match what proceeds equals if what follows equals matches

$mailArray = preg_grep(“%mail(?!woman)%i”, $randomArray);

foreach($mailArray as $result)
{
echo $result, “<br />”;
}

# (?!) – Match what proceeds if what follows doesn’t match

$manArray = preg_grep(“%(?<=mail)woman%i”, $randomArray);

foreach($manArray as $result)
{
echo $result, “<br />”;
}

?>

</body>
</html>

5 Responses to “Web Design and Programming Pt 8 Regex”

  1. Nicolas says:

    Hi Derek,

    Just a quick thought of mine: why are you not working as a software engineer, if you spent so much time programming and like to program aswell ? I’ve read your “About”, marketing and banking are just totally different things 🙂

    Also, could you provide me your email – I would like to question you for some advice.

    Best regards
    Nicolas (Denmark)

  2. Niko says:

    Hi there, great tut! very understandable and very useful..
    just a question..I’m trying to make my own stripping function using regular expression (which are more secure than e simple str_replace), I’ve tried something like:

    preg_replace(“%[\”/\\’]%”, “”, $string);

    to strip quotes, double quotes, backslashes and forwardlashes..the point is that it’s not working..can you point out why?

    thanks a lot!

Leave a Reply

Your email address will not be published.

Google+