Patrik Krehák
Patrik Krehák

Reputation: 2683

PHP regex - string contains 'facebook' with all domains

I need to check, if users input URL contains facebook(.any domain). This is what I have:

preg_match("/.(facebook)+\.[A-Z]{2,4}/", $input);

But on typying www.facebook.com it returns false. Could someone help me with this? I am not very good in regex.

Upvotes: 1

Views: 476

Answers (2)

Satish Sharma
Satish Sharma

Reputation: 9635

you can also try this along with the answer of @stema

if(strpos($url, "facebook") !== FALSE)
{
   echo "exists";
}
else
{
   echo "not exists";
}

Upvotes: 1

stema
stema

Reputation: 93026

That is because you are only matching for uppercase letters in the last part. You may want to make it match case independent by adding a modifier:

preg_match("/.(facebook)+\.[A-Z]{2,4}/i", $input);

The next things are:

  1. you don't need to put "facebook" into a group

  2. You don't need to quantify facebook

  3. if you want to match a dot, then escape it

So end up with this:

preg_match("/\.facebook\.[A-Z]{2,4}/i", $input);

Upvotes: 4

Related Questions