开发者

Only Allow IMDB LINK REGEX solution PHP

开发者 https://www.devze.com 2023-04-07 04:35 出处:网络
I want a regex solution to allow only http://www.imdb.com/title/ttANYNumberWOrdetc/ links Otherwise SHOW us error.. Incorrect link

I want a regex solution to allow only http://www.imdb.com/title/ttANYNumberWOrdetc/ links

Otherwise SHOW us error.. Incorrect link

I am not too good with regex

I just create this petren ..

preg_match('/http:\/\/www.imdb.com\/title\/(.*)\//i开发者_如何学Pythons', 'http://www.imdb.com/title/tt0087469/', $result);

Its show me corect result but i think i missed some thing..

Thanks,


How about something like this: http://(?:www\.)?imdb.com/title/tt[^/]+/.

Example:

<?php

if ( preg_match('#^http://(?:www\.)?imdb\.com/title/tt[^/]+/$#', 'http://www.imdb.com/title/tt0448303/') )
  echo 'Matches' . PHP_EOL;

Explanation:

The regular expression matches a string that starts with http:// followed either by imdb.com or www.imdb.com, then /title/tt followed by any character except for a / and that ends with a /.

The # is the delimiter, the ^ indicated the beginning of the string and the $ the end.


This should work:

if (preg_match("#^(http://www.|https://www.)imdb.com/title/tt([a-zA-Z0-9]+)(?:/)(?:^[a-zA-Z0-9]+)?$#s", 'http://www.imdb.com/title/tt0364845/', $matches)) {
  echo 'yay';
} else {
  echo 'nay';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号