【问题标题】:Explode Request URI展开请求 URI
【发布时间】:2016-02-28 19:16:51
【问题描述】:
$url_current = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

如果 url 当前返回:

http://example.com/part1_part2_part3_part4_part5/

在url中part1到part4总是相等的,part5是可变的。

我如何分解 request_URI 并捕获 part5 并放入 $variable5

【问题讨论】:

    标签: php httprequest


    【解决方案1】:
    <?php
    $url = "http://example.com/part1_part2_part3_part4_part5/";// your example url
    $data = parse_url($url); // parse_url() to get path data
    echo "<pre/>";print_r($data); // print parse_url() data
    $new_data = explode('_',$data['path']); // explode the path data only from parse_url() data 
    $last_param = trim($new_data[count($new_data)-1],'/'); // remove `/` from the data and assign to a variable
    echo $last_param; // print variable value
    ?>
    

    输出:- http://prntscr.com/a9445q

    【讨论】:

    • 谢谢@Anant,它可以工作,如果我需要倒数第二个变量,只需更改为 -2 很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 2012-11-08
    • 1970-01-01
    • 2018-08-02
    相关资源
    最近更新 更多