1

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));
}
1
  • 1
    What's the question? Explode, iterate, possibly trim each value before validating. What doesn't work? Commented Sep 6, 2012 at 4:32

3 Answers 3

1
function isValidURL($url){
   return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

function sanitizeSQL($input) {
   if(get_magic_quotes_gpc() == true){
      $input = stripslashes($input);
   }
   return mysql_real_escape_string(htmlspecialchars($input));
}

$urls = explode("\n", $_POST['urls']);

$errorcount = 0;
foreach($urls as $url) {
   // next line fixes conflict by cleaning here rather then before
   $url = sanitizeSQL($url);
   $result = isValidURL(trim($url));
   // Do something with ur result
   if(!$result) $errorcount++;

}
if($errorcount>0){
  //Failed
} else {
  //Passed
}

Note

Try to avoid mysql_* functions use mysqli_* or PDO.

Sign up to request clarification or add additional context in comments.

6 Comments

This works but it is conflicting with another function (see first post)
What unexpected results are you getting with tat conflicting function ?
Your code works without my function in place but not with, my function "cleans" the textarea input before exploding EG: $links = sanitizeSQL($_POST['urls']); $urls = explode("\n", $links);
Ok sanitize the urls individually after exploding. because if you sanitize before exploding you might wanna use \, as the exploding delimiter i guess!! can you print the value of $links after sanitizing ?
Thank you Deeepak, I put $url = sanitizeSQL($url); inside the foreach loop and its working now.
|
1

Using your example code (array instead of explode) I suggest something like

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');

$result = 'pass';
foreach ($urls as $check) {
  if(!isValidURL($check)) {
    $result = 'fail';
    break;
  }
}

echo $result;

Comments

0

Or maybe a little bit shorter... ?

$urls = array_map('trim', explode("\n", $string));
$validUrls = array_filter(filter_var_array($urls, FILTER_VALIDATE_URL));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.