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:41

strstr didn't exist back then?

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

This must be one of the easiest methods right?

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-08 04:43
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];


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

This seems to work.

查看更多
劫难
4楼-- · 2019-01-08 04:43
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;
}
查看更多
贪生不怕死
5楼-- · 2019-01-08 04:44

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

查看更多
\"骚年 ilove
6楼-- · 2019-01-08 04:44

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

查看更多
Lonely孤独者°
7楼-- · 2019-01-08 04:47
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( strpbrk($url, 'test') ) {
    // Do something...
}
else {
    // Do another thing
}

(>PHP 5)

查看更多
登录 后发表回答