Check if URL has certain string with PHP

2019-01-08 04:37发布

问题:

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'.

回答1:

Try something like this. The first row builds your URL and the rest check if it contains the word "car".

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];


if (strpos($url,'car') !== false) {
    echo 'Car exists.';
} else {
    echo 'No cars.';
}


回答2:

I think the easiest way is:

if (strpos($_SERVER['REQUEST_URI'], "car") !== false){
// car found
}


回答3:

$url = " www.domain.com/car/audi/";
if (strpos($url, "car")!==false){
    echo "Car here";
}
else {
   echo "No car here :(";
}

See strpos manual



回答4:

if( strpos( $url, $word ) !== false ) {
    // Do something
}


回答5:

strstr didn't exist back then?

if(strstr($_SERVER['REQUEST_URI'], "car")) {
   echo "car found";
}

This must be one of the easiest methods right?



回答6:

worked for me with php

if(strpos($_SERVER['REQUEST_URI'], 'shop.php') !== false){
echo 'url contains shop';
}


回答7:

Have a look at the strpos function:

if(false !== strpos($url,'car')) {
    echo 'Car exists!';
}
else {
    echo 'No cars.';
}


回答8:

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];


if (!strpos($url,'car')) {
    echo 'Car exists.';
} else {
    echo 'No cars.';
}

This seems to work.



回答9:

Surely this is the correct way round....

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];


if (!strpos($url,'mysql')) {
echo 'No mysql.'; //swapped with other echo statement
} else {
echo 'Mysql exists.';
}

Otherwise its reporting the opposite way it should...



回答10:

You can try an .htaccess method similar to the concept of how wordpress works.

Reference: http://monkeytooth.net/2010/12/htaccess-php-how-to-wordpress-slugs/

But I'm not sure if thats what your looking for exactly per say..



回答11:

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
}


回答12:

if ( stristr( SITE_HOST, 'your site' ) ) {
    $protocol = ( !empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) ? 'https://' : 'http://';
    $request = ( !empty( $_SERVER['HTTP_X_ORIGINAL_REQUEST'] ) ) ? $_SERVER['HTTP_X_ORIGINAL_REQUEST'] : $_SERVER['REQUEST_URI'];
    echo $protocol .'your site'. $request;
}


回答13:

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( strpbrk($url, 'test') ) {
    // Do something...
}
else {
    // Do another thing
}

(>PHP 5)



回答14:

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>';


标签: php string url