PHP Stripos Check for two Words in a URL string

481 Views Asked by At

I'm currently using PHP Stripos OK to look for a word (parrots) is present within a URL.

However I now need to check that 2 words (green and parrots) are present in a URL, i.e. they must both be present.

Here's the current code:

if (stripos($_SERVER['REQUEST_URI'],'-parrots') !== false) 
        {echo '<div class="clear"></div><a href="http://www.example.com/parrots/" class="btn"> >> Parrots</a>';}

The above works well.

For two words I've tried:

if (stripos($_SERVER['REQUEST_URI'],'-green','-parrots') !== false) 
        {echo '<div class="clear"></div><a href="http://www.example.com/green-parrots/" class="btn"> >> Green Parrots</a>';}

But that doesn't seem to work.

Any ideas?

2

There are 2 best solutions below

0
On

Just use two stripos calls:

<?php
if (stripos($_SERVER['REQUEST_URI'],'-parrots') !== false &&
    stripos($_SERVER['REQUEST_URI'],'-green') !== false) {
    // ...
}
0
On

with stripos() you have needle, haystack and offset

you would be better off looking for

if ( (stripos($_SERVER['REQUEST_URI'],'-green') !== false) && (stripos($_SERVER['REQUEST_URI'],'-parrot') !== false) )

you can use && (and) or || (or) depending on your needs