PHP - 如果页面 URL = "http://example.com/page",显示图像。否则,显示另一张图片

PHP - If page URL = "http://example.com/page", display image. Else, display another image

我有我的模板,如果您在某个页面上,我希望它显示某个图像,例如 http://example.com/test如果您不在该页面上 , 然后我想让它显示另一张图片

我也希望它显示图像如果你在任何子目录中,比如http://example.com/test/stuff

此外,有没有办法在同一代码中对多个页面执行此操作?

很喜欢

if page = example.com/test then display testimg.jpg

if page = example.com/archive then display archive.jpg

else, display defaultimg.jpg

谢谢!

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( strpos($url, 'test') !== false ) {
    echo('<img src="image_path/testimg.jpg">');
}
elseif ( strpos($url, 'archive') !== false ) {
    echo('<img src="image_path/archive.jpg">');
}
else {
    echo('<img src="image_path/defaultimg.jpg">');
}

您还可以使用 strpbrk() 函数并获得更紧凑的代码:(>PHP5)

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( strpbrk($url, 'test') ) {
    echo('<img src="image_path/testimg.jpg">');
}
elseif ( strpbrk($url, 'archive') ) {
    echo('<img src="image_path/archive.jpg">');
}
else {
    echo('<img src="image_path/defaultimg.jpg">');
}