AKG
AKG

Reputation: 3296

Regular expressions: does not contain

I know this question has been asked many times on SO, but I can't seem to get this to work.

I'm trying to use regular expressions to find matches for any Facebook URL, but not when the URL contains "plugins/like" (as in "http://www.facebook.com/plugins/like")

I've come out with the following, and I'm not quite sure why it is not working:

https?://(www)?\.facebook\.[a-zA-Z]{2,4}/((?!plugins/like).)

Am I making a very obvious mistake? Sorry if it doesn't make sense at all, but I've only trying a hand at PHP for the past five months.

Thank you very much for your time and your help.

Edit: I'm getting matches for any FB URL so far, but it isn't excluding anything that contains plugins/like

Upvotes: 2

Views: 172

Answers (4)

MarcDefiant
MarcDefiant

Reputation: 6889

here is a solution using strpos()

$url = 'http://www.facebook.com/test/plugins/like?somestuff';

$matches = array();

if(preg_match('#^https?://(?:www)?\.facebook\.[a-zA-Z]{2,4}/(.*)$#', $url, $matches)
   && strpos($matches[1], "plugins/like") === false) {
  // ok
} else {
  // nope
}

Upvotes: 1

Igor Chubin
Igor Chubin

Reputation: 64603

All is ok with your re (and it works; I've checked). The only thing I would change is write

(?!.*plugins/like)

instead of (?!plugins/like).. That is for cases when plugins goes not direct after the facebook.com/.

Upvotes: 0

Miraage
Miraage

Reputation: 3464

$urls = array(
    'http://www.facebook.com/',
    'https://facebook.com/plugins/foo',
    'http://facebook.ru/plugins/like',
);

$pattern = '#^https?://(www\.)?facebook\.[a-z]{2,4}/(?!plugins/like)#';

foreach ($urls as $url) {
    echo preg_match($pattern, $url) . PHP_EOL;
}

Okay?

Upvotes: 2

Summoner
Summoner

Reputation: 1407

You forgot to esacpe special characters in the pattern. Characters like : and / must be following a backslash.

https?\:\/\/(www)?\.facebook\.[a-zA-Z]{2,4}\/((?!plugins\/like).)

Upvotes: 0

Related Questions