$path = substr($_SERVER['REQUEST_URI'], 1);
$parts = explode('/', $path);
$count = count($parts);
echo "<script>console.log('URL Path: $path');</script>";
echo "<script>console.log('Parts Length: $count');</script>";
foreach($parts as $i =>$key) {
$i >0;
echo "<script>console.log('Part $i). $key');</script>";
}
echo "<script>console.log('Test: $key[3]');</script>";
经测试的网址:domain.com/Projects/A/B/C
网址路径:项目/ A / B / C
零件长度:4
第0部分)。项目
第1部分)。甲
第2部分)。乙
第3部分)。 ç
测试:
从我的结果中可以看出,我的echo "<script>console.log('Test: $key[3]');</script>";
尝试记录特定部分不起作用。如何从爆炸的字符串中获取part[X]
?
答案 0 :(得分:2)
你有部分价值(而不是foreach之外的$ key)
$path = substr($_SERVER['REQUEST_URI'], 1);
$parts = explode('/', $path);
$count = count($parts);
echo "<script>console.log('URL Path: $path');</script>";
echo "<script>console.log('Parts Length: $count');</script>";
foreach($parts as $i =>$key) {
$i >0;
echo "<script>console.log('Part $i). $key');</script>";
}
echo "<script>console.log('Test: " . $parts[3] . " ');</script>";