Check if URL has certain string with PHP

后端 未结 12 732
自闭症患者
自闭症患者 2020-11-30 17:19

I would like to know if some word is present in the URL.

For example, if word car is in the URL, like www.domain.com/car/ or www.domain.com/car/audi/ it would echo \

12条回答
  •  心在旅途
    2020-11-30 17:51

    This worked for me:

    // Check if URL contains the word "car" or "CAR"
       if (stripos($_SERVER['REQUEST_URI'], 'car' )!==false){
       echo "Car here";
       } else {
       echo "No car here";
       }
    
    If you want to use HTML in the echo, be sure to use ' ' instead of " ".
    I use this code to show an alert on my webpage https://geaskb.nl/ 
    where the URL contains the word "Omnik" 
    but hide the alert on pages that do not contain the word "Omnik" in the URL.

    Explanation stripos : https://www.php.net/manual/en/function.stripos

提交回复
热议问题