从 url CodeIgniter 获取段但不是第一个
Get segment from url CodeIgniter but not first
我知道我可以像这样从 url 中获取所有片段
假设我有这个例子link
www.example.com/de/products.html
像这样使用url_helper:
$data['url'] = $this->uri->uri_string();
我会得到这样的价值
de/products
但我不需要第一段 de,只需要 products,问题是
我不知道会有多少段,我只需要删除第一个
是否有可能在 CI 中使用 url 助手忘记第一个片段?
CI 中没有 URL 助手来忘记第一段。但是,您可以轻松地制作一个自定义的并将 @Hikmat 的答案放在核心文件夹中的 application/helpers/MY_url_helper.php 下方。
例如
function my_forget_first_segment() {
$data= $this->uri->uri_string();
$arr=explode('/', $data);
array_shift($arr);
$uri=implode('/',$arr);
return $uri;
}
编辑答案前。
You need to try this
$second_segment = $this->uri->segment(2);
来自 Codeigniter 文档 -
$this->uri->segment(n);
允许您检索特定片段。其中 n 是您要检索的段号。段从左到右编号。例如,如果您的完整 URL 是这样的:
http://example.com/index.php/news/local/metro/crime_is_up
段号是这样的:
1. news
2. local
3. metro
4. crime_is_up
可选的第二个参数默认为 NULL 并允许您在请求的 URI 段丢失时设置此方法的 return 值。例如,这将告诉方法在失败时 return 数字零:
$product_id = $this->uri->segment(3, 0);
这样试试...
使用 php 的 explode()
函数将 url 字符串作为 array.Then 应用数组的 array_shift()
函数,该函数始终从中删除第一个元素数组。
代码如下所示
$data= $this->uri->uri_string();
$arr=explode('/', $data);
array_shift($arr);
//print_r($arr);
然后使用 php 的 implode()
方法获得 URI
而无需先 segment.Hope 它会起作用...
$uri=implode('/',$arr);
echo $uri;
示例:
<?php
$data=$this->uri->segment(2);
$val=explode('.', $data);
echo $val[0];
?>
我知道我可以像这样从 url 中获取所有片段
假设我有这个例子link
www.example.com/de/products.html
像这样使用url_helper:
$data['url'] = $this->uri->uri_string();
我会得到这样的价值
de/products
但我不需要第一段 de,只需要 products,问题是 我不知道会有多少段,我只需要删除第一个
是否有可能在 CI 中使用 url 助手忘记第一个片段?
CI 中没有 URL 助手来忘记第一段。但是,您可以轻松地制作一个自定义的并将 @Hikmat 的答案放在核心文件夹中的 application/helpers/MY_url_helper.php 下方。
例如
function my_forget_first_segment() {
$data= $this->uri->uri_string();
$arr=explode('/', $data);
array_shift($arr);
$uri=implode('/',$arr);
return $uri;
}
编辑答案前。
You need to try this
$second_segment = $this->uri->segment(2);
来自 Codeigniter 文档 -
$this->uri->segment(n);
允许您检索特定片段。其中 n 是您要检索的段号。段从左到右编号。例如,如果您的完整 URL 是这样的:
http://example.com/index.php/news/local/metro/crime_is_up
段号是这样的:
1. news
2. local
3. metro
4. crime_is_up
可选的第二个参数默认为 NULL 并允许您在请求的 URI 段丢失时设置此方法的 return 值。例如,这将告诉方法在失败时 return 数字零:
$product_id = $this->uri->segment(3, 0);
这样试试...
使用 php 的 explode()
函数将 url 字符串作为 array.Then 应用数组的 array_shift()
函数,该函数始终从中删除第一个元素数组。
代码如下所示
$data= $this->uri->uri_string();
$arr=explode('/', $data);
array_shift($arr);
//print_r($arr);
然后使用 php 的 implode()
方法获得 URI
而无需先 segment.Hope 它会起作用...
$uri=implode('/',$arr);
echo $uri;
示例:
<?php
$data=$this->uri->segment(2);
$val=explode('.', $data);
echo $val[0];
?>