使用PHP根据URI更改href

3

我要让主菜单栏的链接根据访问者当前的页面变化而变化。

我从下面这个开始:

 $path = $_SERVER['REQUEST_URI'];

当然,这将返回像以下这样的东西:
  • /subfolder/page.html
  • /subfolder1/subfolder2/page.html
  • /page.html
我需要获取第一个斜杠后面的任何内容。我尝试过使用explode函数,但是我不知道如何处理结果数组。我也尝试了写正则表达式,但是感觉有更优雅的解决方案。
然后我需要构建我的switch语句,大致如下:
switch ($path)
{
case '/subfolder0':
  $link = $root_url.'/subfolder0/anotherfolder/page.html';
  break;
case '/subfolder1':
  $link = $root_url.'/subfolder1/page.html';
  break;
default:
  $link = $root_url.'/subfolder2/page.html';
}

最后,我应该使用if...else if代替switch吗?
谢谢您的时间!

你的意思是指“...第一个斜杠后面[但在第二个斜杠之前]的任何内容”吗? - Andrew Senner
在某些情况下,可能不会有第二个“/”。 - Amelia Earhart
4个回答

2
抓取第一个斜杠之后的所有内容:
strstr($_SERVER['REQUEST_URI'], '/');

或者,使用正则表达式:
preg_match('#(/.*)#', $_SERVER['REQUEST_URI'], $matches);  // $matches[1] will be the path

就开关而言,如果/否则语句在您的情况下最不优雅,开关并不差,但个人建议使用关联数组:

$mapping = array('/subfolder0' => $root_url.'/subfolder0/anotherfolder/page.html', 'etc' => 'etc');
$link = $mapping($path);

这样可以将映射保留在另一个文件中以实现组织,并通过将配置与实现分开,使其更易于维护。

1
使用 explode 并不是一个坏主意,如果你对 URI 的所有部分感兴趣,你应该看一下 explode 文档
它的用法如下:
$exploded = explode('/','/path/to/page.html');

echo $exploded[0]; // Will print out '' (empty string)
echo $exploded[1]; // Will print out 'path'
echo $exploded[2]; // Will print out 'to'
echo $exploded[3]; // Will print out 'page.html'

然而据我所知,您想要用第一个字符后面的任何内容(始终为“/”)替换链接,您可以使用substr,如下所示:

// Get whatever is after the first character and put it into $path
$path = substr($_SERVER['REQUEST_URI'], 1); 

在您的情况下,这不是必需的,因为您能够预测字符串开头有一个反斜杠。
我还建议使用关联数组来替换URL。
我会像这样实现整个过程(根据您的要求删除第一个反斜杠):
// Define the URLs for replacement
$urls = array(
    'subfolder0' => '/subfolder0/anotherfolder/page.html',
    'subfolder1' => '/subfolder1/page.html'
);

// Get the request URI, trimming its first character (always '/')
$path = substr($_SERVER['REQUEST_URI'], 1);

// Set the link according to $urls associative array, or set
// the default URL if not found
$link = $urls[$path] or '/subfolder2/page.html';

或者使用explode函数,只获取URI的第一部分:

// Get the parts of the request
$requestParts = explode('/', $_SERVER['REQUEST_URI']);

// Set the link according to $urls associative array, or set
// the default URL if not found
$link = $urls[$requestParts[1]] or '/subfolder2/page.html';

1

分析了OP的问题后,我认为他/她的意思是将其表述为“第一个'/'之后,但在第二个'/'之前的所有内容。这是我的答案:

您可以尝试使用此正则表达式:

<?php

/*
 *  Regex: /((\w+?|\w+\.\w+?)(?!^\/))(?=\/.*$|$)/
 */

$paths = array(
    '/subfolder9/',
    '/subfolder/page.html',
    '/subfolder1/subfolder2/page.html',
    '/page.html'
);

foreach ($paths as $path) {
    preg_match("/((\w+?|\w+\.\w+?)(?!^\/))(?=\/.*$|$)/", $path, $matches);
    debug($matches);
}

// $matches[1] will contain the first group ( ) matched in the expression.
// or "subfolder<#>" or "<page>.<ext>"

// The loop results is as follows:
Array
(
    [0] => subfolder9
    [1] => subfolder9
    [2] => subfolder9
)

Array
(
    [0] => subfolder
    [1] => subfolder
    [2] => subfolder
)

Array
(
    [0] => subfolder1
    [1] => subfolder1
    [2] => subfolder1
)

Array
(
    [0] => page.html
    [1] => page.html
    [2] => page.html
)

?>

注意:此方法仅适用于支持预测先行和负向先行的正则表达式(零宽度正向和负向先行是示例中使用的类型)。
这是一个非常好的正则表达式速查表,没有它我就不会编程。 Regular-Expressions.info - (点击查看)

1
您可以使用 dirname 函数来获取您想要的内容:
$path = dirname('/subfolder/page.html'); // returns '/subfolder'
$path = dirname('/subfolder1/subfolder2/page.html'); // returns '/subfolder1'
$path = dirname('page.html'); // returns '.'

编辑:基于正则表达式的解决方案:

$path = preg_replace('#^(/[^/]*).*$#', '$1', '/subfolder/page.html' )

如果文件被假定,URI 最终变成 "/subfolder/" 会怎样?那将返回 "/". 我提供的正则表达式可以处理这两种情况。 - Andrew Senner
@Drewdiddy611:感谢您指出,已添加编辑部分以更好地处理这些边缘情况。 - anubhava

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接