问题
I have an auto site and I am having a feature called SAVE SEARCH, which saves the search a user has made on the site for future use. What I am doing is that I am saving entire url in the database.
But I need to validate it as a valid url. It looks like this.
http://www.anywebsite.com/cars.php?postcode=CV2+5AS&distance=&make=&min_price=&max_price=&category_type=&body_type=&fuel=&colour=&transmission=&year_of_registration=&mileage=&engine=&doors=&seller=&keywords=&sort=PRICE_LOWEST&referer_url=http%253A%252F%252Flocalhost%252Fselling%252Fcars.php&trader_id=0&case=ADVANCE
Can anyone please provide me with any idea as to how I can achieve it?
I have preg_match which is here.
if (!preg_match('/^https?:(\/\/)?(www\.)?([a-zA-Z0-9_%]*)\b\.[a-z]{2,4}(\.[a-z]{2})?/', $fields[$field_name]))
But it only validates urls such as http://www.anywebsite.com, whereas I need to validate the entire above url.
Any help will be highly appreciated.
回答1:
There shouldn't be a reason why you would need to code up URL validation by hand, this problem has been solved.
Take a look at php's filter_var().
Example of URL validation: http://www.w3schools.com/php/filter_validate_url.asp
$url = "http://www.anywebsite.com/cars.php?postcode=CV2+5AS&distance=&make=&min_price=&max_price=&category_type=&body_type=&fuel=&colour=&transmission=&year_of_registration=&mileage=&engine=&doors=&seller=&keywords=&sort=PRICE_LOWEST&referer_url=http%253A%252F%252Flocalhost%252Fselling%252Fcars.php&trader_id=0&case=ADVANCE";
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo "URL is not valid";
}
else {
echo "URL is valid";
}
回答2:
You should use filter_vars() with FILTER_VALIDATE_URL filter as suggested in other answers. However, if you want to use RegEx, you must see In search of the perfect URL validation regex blog by @Mathias Bynens
I'll only add one RegEx by @diegoperini that supports all the URL patterns.
^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$
回答3:
why reinvent the wheel when you can do filter_var()? then use the filter FILTER_VALIDATE_URL
filter_var
is for PHP 5 >= 5.2.0 Also you might want to add FILTER_FLAG_HOST_REQUIRED
to have the domain name required, which I believe you will need and you might check out the other flags to see if you need them.
Example:
$url = 'http://www.anywebsite.com/cars.php?postcode=CV2+5AS&distance=&make=&min_price=&max_price=&category_type=&body_type=&fuel=&colour=&transmission=&year_of_registration=&mileage=&engine=&doors=&seller=&keywords=&sort=PRICE_LOWEST&referer_url=http%253A%252F%252Flocalhost%252Fselling%252Fcars.php&trader_id=0&case=ADVANCE';
if(filter_var($url, FILTER_VALIDATE_URL)){
#do stuff here
}else{
#invalid
}
Working example: http://ideone.com/N84VGF
if you have a older version of PHP you can use parse_url() but you'd have to add a regex/validation because it doesn't validate if its a real/valid url.
来源:https://stackoverflow.com/questions/15595520/how-to-validate-a-url-with-lots-of-querystrings