views:

227

answers:

2

Hey there, I was wondering how I could check a string broken into an array against a preg_match to see if it started with www. I already have one that check for http://www.

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

$stringToArray = explode(" ",$_POST['text']);

  foreach($stringToArray as $key=>$val){
  $urlvalid = isValidURL($val);
  if($urlvalid){
  $_SESSION["messages"][] = "NO URLS ALLOWED!";
  header("Location: http://www.domain.com/post/id/".$_POST['postID']);
     exit();
     }
     }

Thanks! Stefan

A: 

Try implode($myarray, '').strstr("www.")==0. That implodes your array into one string, then checks whether www. is at the beginning of the string (index 0).

Hawkcannon
I explode the string at first as the url might be half way through it e.g. hello how are you www.google.com
Stefan
+1  A: 

You want something like:

%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i

this is using the | to match either http:// or www at the beginning. I changed the delimiter to % to avoid clashing with the |

Igor