从网址CodeIgniter获取细分,但不是第一个

时间:2016-12-23 09:13:41

标签: codeigniter url codeigniter-3

我知道我可以像这样从网址获取所有细分

假设我有这个示例链接

www.example.com/de/products.html

像这样使用url_helper:

$data['url'] = $this->uri->uri_string();

我会得到这样的价值

de / products

但我不需要第一段 de ,只有产品,问题是 我不知道会有多少段,我只需要删除第一个

是否有可能忘记CI中的url helper的第一段?

3 个答案:

答案 0 :(得分:1)

试试这个......

使用php的explode()函数将url字符串作为数组。然后应用数组的array_shift()函数,该函数始终从数组中删除第一个元素。

代码如下所示

        $data= $this->uri->uri_string();
        $arr=explode('/', $data);
        array_shift($arr);
        //print_r($arr);

然后使用php的implode()方法获取没有第一段的URI。希望它能正常运行......

$uri=implode('/',$arr);
echo $uri;

答案 1 :(得分:0)

There is no URL helper in the CI to forget the first segment. However you can easily make a custom one and put @Hikmat's answer below it in the application/helpers/MY_url_helper.php in the Core folder.

e.g.

function my_forget_first_segment() {
    $data= $this->uri->uri_string();
        $arr=explode('/', $data);
        array_shift($arr);
        $uri=implode('/',$arr);
        return $uri;
}

Before Edit answer.

You need to try this 

$second_segment = $this->uri->segment(2);

From Codeigniter documentation -

$this->uri->segment(n);

Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:

http://example.com/index.php/news/local/metro/crime_is_up

The segment numbers would be this:

1. news
2. local
3. metro
4. crime_is_up

The optional second parameter defaults to NULL and allows you to set the return value of this method when the requested URI segment is missing. For example, this would tell the method to return the number zero in the event of failure:

$product_id = $this->uri->segment(3, 0);

答案 2 :(得分:0)

示例:

{{1}}