My preg_match isn't work with the variable - why is this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

My preg_match isn't work with the variable - why is this?

Post by simonmlewis »

Code: Select all

$email = isset($_POST['email']) ? $_POST['email'] : null;
$find = "*";
if(preg_match("/$find-\d/", $email))  {
    echo "matched";
}
This isn't working.
[text]Warning: preg_match(): Complication failed: nothing to repeat at offset 0.[/text]

I'm trying to find * in the email address. and act upon that.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: My preg_match isn't work with the variable - why is this

Post by Celauran »

* has special meaning in regular expressions, so you'll need to escape it.

Code: Select all

preg_match('/\*-\d/', $email);
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: My preg_match isn't work with the variable - why is this

Post by simonmlewis »

Yeah sorry to say, I worked that out a few hours ago, but forgot to post it here.
Thanks tho.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply