problem with regxp
My php script should validiate address of websites, that user type into the form. Adress should look like this:
http://example开发者_运维百科.com/example/{some numbers}/
or
http://example.com/example/{some numbers}
And I have think about something like this, but it doesn't work:
/^(http:\/\/)?example\.com\/example\/\d{1}(\/?)$/
Can you show me where I'm wrong?
\d{1}
means: ONLY one digit. Maybe you want to do \d+
(at least one digit)
hm, some numbers or some number? In case you meant the first case, the greedy modifier is wrong, because it tells your regexp to only match exactly one decimal. Besides, it works just fine here.
Why not use PHP's built-in filter mechanism for this?
filter_var('http://example.com/foo', FILTER_VALIDATE_URL); // returns the URL
filter_var('not an URL', FILTER_VALIDATE_URL); // returns false
Explore some examples.
In addition, there is parse_url which lets you split any URL into it's components.
精彩评论