我有这个代码:
<?php $url = JURI::getInstance()->toString();
if ($url == "http://example.com/news/latest/"){
echo "This is latest page";
} else {
echo "This is not latest page";
}
?>
我想做的是http://example.com/news/latest/',如何选择/latest/下的页面/项目。如果它更有意义的话,这里有一个语法:
if ($url == "http://example.com/news/latest/" + ANYTHING UNDER THIS)
我不能使用不等于($url!=),因为它将包括不等于/latest/的其他父页。我只想要它下面的东西。如果有人理解它,我需要帮助如何将它放入代码中。
更新:我想做的是,如果页面是example.com/news/latest,它会回复"latest"。例如,如果我在example.com/news/latest/subpage1/subpage2中,它会回应"你所在的页面位于最新页面下"。任何超出"最新页面"的内容都会回应。
$str = 'example.com/news/latest/dfg';
preg_match('/example.com'/news'/([^'/]+)'/?(.*)/', $str, $page);
if(isset($page[2]) && $page[2])
echo 'You are under: ' , $page[1];
elseif(isset($page[1]))
echo 'At: ' , $page[1];
else
echo 'Error';
编辑:澄清后切换为正则表达式。
使用正则表达式:
$matches = array();
if((preg_match('#http://example'.com/news/latest/(.*)#', $url, $matches)) === 1) {
if(strlen($matches[0]) > 0) {
echo "You're at page: $matches[0]";
} else {
echo "You're at the root";
}
} else {
// Error, incorrect URL (should not happen)
}
EDIT:已修复,未经测试,因此您可能需要对其进行一点调整