Check if URL has certain string with PHP

2019-01-08 04:28发布

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 'car is exist' and if there's nothing it would echo 'no cars'.

标签: php string url
14条回答
我只想做你的唯一
2楼-- · 2019-01-08 04:48

you can use this function as this code sir

 if((strpos($_SERVER['REQUEST_URI'],'car') !== false))
        echo'<h3 class=" edit-top">Car Exist</h3>';
    else
        echo'<h3 class=" edit-top">Not</h3>';
查看更多
唯我独甜
3楼-- · 2019-01-08 04:49

worked for me with php

if(strpos($_SERVER['REQUEST_URI'], 'shop.php') !== false){
echo 'url contains shop';
}
查看更多
Deceive 欺骗
4楼-- · 2019-01-08 04:50
if( strpos( $url, $word ) !== false ) {
    // Do something
}
查看更多
干净又极端
5楼-- · 2019-01-08 04:51

Have a look at the strpos function:

if(false !== strpos($url,'car')) {
    echo 'Car exists!';
}
else {
    echo 'No cars.';
}
查看更多
可以哭但决不认输i
6楼-- · 2019-01-08 04:56

The URL parameters and received from a global variable called $_GET which is in fact an array. So, to know if a URL contains a parameter, you can use the isset() function.

if (isset($_GET['yourparametername'])) {
//The parameter you need is present
}

Afterwards, you can create separate array of such parameter you need to attach to a URL.

For example:

if(isset($_GET['param1'])) {
\\The parameter you need is present
$attachList['param1'] = $_GET['param1'];
}

if(isset($_GET['param2'])) {
$attachList['param2'] = $_GET['param2];
}

Now, to know whether or not, you need a ? symbol, just count this array

if(count($attachList)) {
    $link .= "?";
    // and so on
}

Update:

To know if any parameter is set, just count the $_GET

if(count($_GET)) {
     //some parameters are set
}
查看更多
Juvenile、少年°
7楼-- · 2019-01-08 05:03
$url = " www.domain.com/car/audi/";
if (strpos($url, "car")!==false){
    echo "Car here";
}
else {
   echo "No car here :(";
}

See strpos manual

查看更多
登录 后发表回答