开发者

Rapidshare URL not matching correctly

I'm trying to make sure that a Rapidshare URL is valid when a user submits it through my form.

This is the regex that I've come up with so far:

http://rapidshare.com/files/[0-9]+/[a-zA-Z0-9\._-]+

A rapidshare link looks like this:

http://rapidshare.com/files/168501977/some_random-file.zip

My pattern matches, but not entirely correctly. For example, if we use this input:

http://rapidshare.com/files/168501977/some_random-file.zip£%^$

It will still match using the PHP function preg_match(), and let it 开发者_StackOverflow中文版go through, even though there are illegal symbols on the end of the URL. I want the pattern to match the entire input, and not just a random length that matches.

Any help would be appreciated, cheers!


You need to anchor the regex pattern. Use ^ to anchor the beginning and $ to anchor the end. So the pattern becomes:

^http://rapidshare.com/files/[0-9]+/[a-zA-Z0-9\._-]+$

This prevents a partial match of the string like the example is generating.


Validate the start and the end of your string using ^ and $. Example:
^ht{2}p:\/{2}rapidshare\.com\/files\/\d+\/[\.a-zA-Z_-]+$

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜