-3

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;
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
user2477139
  • 608
  • 1
  • 6
  • 21
  • you realize there's a parse error here – Funk Forty Niner Dec 25 '17 at 15:03
  • semicolon was missing but that is not the issue, i added it.my question is not regarding parsing or syntax error – user2477139 Dec 25 '17 at 15:25
  • you're going to have to describe in detail what results you're looking to get as opposed to what you're getting now. I've tested your code and I'm getting results. Your edit and the original post didn't describe that and you can't expect us/me to figure out what you're looking to get. – Funk Forty Niner Dec 25 '17 at 15:27
  • In order to validate_url you must have... http:// www.google.com – halojoy Dec 25 '17 at 15:48
  • I reopened your question earlier and you didn't bother to ping me back nor responded to the answer given. @user2477139 The only reason I saw the edit is because I revisited it. – Funk Forty Niner Dec 25 '17 at 16:12

1 Answers1

0

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');
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141