th,substr就会返回给我们从$start开始直到字符串末尾的字符串),正如每一个优秀的C
程序员告诉你的那样,代表字符串开始的位置开始的数字是“0”而不是“1”。
函数strrpos($string, $what)告诉我们字符串$what在变量$string中最后一次出现的位置,我们可以通过它找出文件名中最后一个下划线的位置在哪,同理,接着的strpos($string, $what)告诉我们“.html”首次出现的位置。我们通过运用这三个函数取得在最后一个“_”和“.html”之间的数字(代码中的strpos()+1代表越过“_”自己)。
剩下的部分很简单,首先为上页和下页构造文件名:
*/
$partial_path = substr($page_file, 0, strrpos($page_file, "_"));
$prev_page_file = $partial_path . "_" . (string)($page_num-1) . ".html";
$next_page_file = $partial_path . "_" . (string)($page_num+1) . ".html";
/*
(string)($page_num+1)将数学运算$page_num+1的结果转化为字符串类型,这样就可以用来与其他字串最终连接成为我们需要的文件名。
*/
/*
现在检查文件是否存在(这段代码假设所有的文件都位于同样的目录下),并最终给出构成页面导航栏的HTML代码。
*/
$prev_exists = file_exists($prev_page_file);
$next_exists = file_exists($next_page_file);
if ($prev_exists)
{
print "<a href="$root/$prev_page_file">previous</a>";
if ($next_exists)
{
print " | ";
}
}
if ($next_exists)
{
print "<a href="$root/$next_page_file">next</a>";
}
?>