dongshun5963 2017-02-05 16:57
浏览 6
已采纳

使用PHP爆炸字符串

PHP Script:

$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>";

Outcome:

Tested URL: domain.com/Projects/A/B/C

URL Path: Projects/A/B/C

Parts Length: 4

Part 0). Projects

Part 1). A

Part 2). B

Part 3). C

Test:

Question:

As you can see from my outcome, my echo "<script>console.log('Test: $key[3]');</script>"; attempt to log a specific part does not work. How can I get part[X] from my exploded string?

  • 写回答

1条回答 默认 最新

  • dsen53898 2017-02-05 17:03
    关注

    You have your value in parts (and not in $key outside the foreach)

        $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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 水下机器人的半物理仿真研究
  • ¥15 微服务假死,一段时间后自动恢复,如何排查处理
  • ¥50 webrtc-streamer TCP rtsp
  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库