In 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:
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>
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)
And of course: thanks, for your very nice tutorials. They are very good compared to other tutorials on the net ! Like your teaching style. Thanks !
You’re very welcome. I’m just happy to find so many nice people 🙂
My about is old. I don’t work for anyone anymore. I make my money off of online stores I run and I do some consulting work making Android apps. I hate having a boss 🙂
My email derekbanas@verizon.net
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!