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.
I explode the string at first as the url might be half way through it e.g. hello how are you www.google.com
Explode the string and use a foreach
statement.
Eg:
$string = "hello how are you www.google.com";
$string = explode(" ", $string);
foreach ($string as $word){
if ( (strpos($word, "http://") === 0) || (strpos($word, "www.") === 0) ){
// Code you want to excute if string is a link
}
}
Note you have to use the ===
operator because strpos
can return, will return a 0
which will appear to be false
.