检查文件名是否包含字符串

时间:2015-12-09 11:05:33

标签: php string if-statement

我有一个函数,我想只在url以特定格式启动时激活,但具有可变结尾。

    $actual_link =  "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 

    if ($actual_link == "http://localhost:1234/WebDevelopment/Bathrooms/index.php") {
        select_imagex("SELECT * FROM page_image WHERE pageID = 1"); 
    }
    else if ($actual_link == "http://localhost:1234/WebDevelopment/Bathrooms/") {
        select_imagex("SELECT * FROM page_image WHERE pageID = 1"); 
    }

    /* section which I have issues with */
    /* x = changing variable */
    else if ($actual_link == "http://localhost:1234/WebDevelopment/Bathrooms/news.php/id=x") {
        echo "bob";
    } else {
        select_image("SELECT * FROM page_image WHERE pageID = ?"); 
    }

2 个答案:

答案 0 :(得分:1)

只需将链接与变量$x连接即可。 不要忘记设置变量$x

$actual_link =  "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 

if ($actual_link == "http://localhost:1234/WebDevelopment/Bathrooms/index.php") {
    select_imagex("SELECT * FROM page_image WHERE pageID = 1"); 
}
else if ($actual_link == "http://localhost:1234/WebDevelopment/Bathrooms/") {
    select_imagex("SELECT * FROM page_image WHERE pageID = 1"); 
}

/* section which I have issues with */
/* x = changing variable */

else if ($actual_link == "http://localhost:1234/WebDevelopment/Bathrooms/news.php/id=".$x) {
    echo "bob";
} else {
    select_image("SELECT * FROM page_image WHERE pageID = ?"); 
}

答案 1 :(得分:0)

你可以检查字符串的前59个字符是否相同:

else if(substr(0, 59, $actual_link) == "http://localhost:1234/WebDevelopment/Bathrooms/news.php/id=" )

或者您可以检查字符串是否包含带有strpos的URL

else if(strpos($actual_link, "http://localhost:1234/WebDevelopment/Bathrooms/news.php/id=") !== false)

如果您想获得x,您还可以获得链接后面的子字符串:

$x = substr($actual_link, 59);