I am trying to isolate link from array but in foreach loop it does not work for me.it cosider both elements as a link.
i just want to hyper link google.com and not bakery text but i am getting link on both so if part is not working and its considering bakery as a link.
$services=array('Bakery','www.google.com');
foreach($services as $service):
if (!filter_var($service, FILTER_VALIDATE_URL) === false) {
$service = $service;
} else {
$service = '<a href='.$service.'>'.$service.'</a>';
}
echo $service;
endforeach;
The problem here is with the following statement:
if (!filter_var($service, FILTER_VALIDATE_URL) === false)
^ ^^^^^
You're using a double negative here, being the !
operator which means "not" and you're using "false".
Either remove the !
or change the "false" to "true".
You're also going to need to add http://
to the url you wish to use in order to validate properly.
$services=array('Bakery','www.google.com');
will fail for the Google link. If you want it to validate, you will need to change it to:
$services=array('Bakery','http://www.google.com');