Hello, i have an javascript which i want to show only if URL do NOT contains certain phrasses. Like include javascript in webpage only if URL is free from words: cars, motorcycles So far i found this code, but it is only for one word: $url = 'http://' . $SERVER['SERVERNAME'] . $SERVER['REQUESTURI']; if (false !== strpos($url,'car')) { echo 'Car exists.'; } else { echo 'No cars.'; } Code (markup):
These phrases, are they part of a query, or the actual Url? Ie, is it something like http://www.example.com/car or http://www.example.com/index.php?search=car or something similar?
Assuming your code is correct, you could do this for multiple words <?php $url = 'http://' . $SERVER['SERVERNAME'] . $SERVER['REQUESTURI']; $match = false; $wordlist = array('motorcycles','car'); foreach ($wordlist as $w) { if (false !== strpos($url,'$w')) { $match = true; } } if ($match) { echo 'At least one word matched'; } else { echo 'No match'; } PHP:
Thank you, i tried that code, but it appears it do not recognize partial phrasses not words, i mean if url is fast-cars-on-sale , and my word is "car", it will still show "No match"
Just found one javascript in the meantime and it works beautifully: <div id="conditionalbox"> Visible only if phrasses do not exist in URL </div> <script> if (/car|motorcycle/.test(window.location.href)) { document.getElementById('conditionalbox').style.display = 'none'; } </script> HTML:
Sorry about that, I had some quotes which I forgot to remove, you will see that this works. $url = 'http://fast-cars-on-sale'; $match = false; $wordlist = array('motorcycles','car'); foreach ($wordlist as $w) { if (false !== strpos($url,$w)) { $match = true; } } if ($match) { echo 'At least one word matched'; } else { echo 'No match'; } PHP: