MegaHertz
MegaHertz

Reputation: 25

PHP If URL Issue

Ok I have an issue with an if php command.

{php}
$searchloadpage = "/search/Car/";
$listingloadpage = "/listing/%";
$currentpage = $_SERVER['REQUEST_URI'];
if($searchloadpage==$currentpage) { 
include('searchload.php');
}
if($listingloadpage==$currentpage) { 
include('searchload.php');
} else {

}
{/php}

Now the questions is how do you make it so with the $listingloadpage = "/listing/HERE" the HERE part I need to make it so that anything that is after listing it will take.

Example: /listing/1222.html or any thing that is after /listing/ it will load my searchload.php file. I think its called an wildcard code where the php will think /listing/and any thing after this listing directory.

Hope that was clear. :(

Upvotes: 0

Views: 74

Answers (2)

njord
njord

Reputation: 1288

You sould use the regex to validate /listing/123.html, listing/abc.html, listing/abc...

<?php
    $searchloadpage = "/search/Car/";
    $listingloadpage = "/\/listing\/.+/";

    $currentpage = $_SERVER['REQUEST_URI'];

    if($searchloadpage == $currentpage) {
        include('searchload.php');

    } else if (preg_match($listingloadpage, $currentpage)) {
        include('searchload.php');

    // something else
    } else {

    }
?>

And, for example, if you only want /listing/123.html and not /listing/somethingelse, you'd want:

$listingloadpage = "/\/listing\/[0-9]+\.html/";

Upvotes: 0

jeroen
jeroen

Reputation: 91734

You can use stristr

$listingloadpage = "/listing/";
if (stristr($currentpage, $listingloadpage) !== false) {
  ...

That will work if /listing/ appears anywhere in the url. If you need it at the start:

if (stristr($currentpage, $listingloadpage) === 0) {

Note that I have removed the % character from your string.

Upvotes: 1

Related Questions