I'm writting a small script where the user adds urls to a textarea, I found this litttle function to validate single links:
function isValidURL($url){
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}
but I need it to validate an array of links
I use explode to seperate each like this:
$urls = explode("\n", $_POST['urls']);
I understand that I need to loop the array of urls using a foreach but how can I get the foreach $var inside my elseif
Example:
function isValidURL($url){
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $valid);
}
$urls = array('http://iamvalid.com','iamnotvalid.com','http://iamvalidaswell.com');
foreach ($urls as $check) {
$validate = isValidURL($check);
}
if($check != $validate) {
// If fail
$result = 'fail';
} else {
// If pass
$result = 'pass';
} // if
echo $result;
(used array instead of expode for demo purposes)
Update: Deepaks answer works great but conflicts with my mysql injection prevention function
Conflicting function:
function sanitizeSQL($input) {
if(get_magic_quotes_gpc() == true){
$input = stripslashes($input);
}
return mysql_real_escape_string(htmlspecialchars($input));
}
trimeach value before validating. What doesn't work?