【问题标题】:How to get the first 2URI only using codeigniter php?如何仅使用 codeigniter php 获取第一个 2URI?
【发布时间】:2016-07-04 04:07:23
【问题描述】:

大家好,我想获取前 2 个 URI,因为我不需要第 3-n 个 URI,

这是一个示例网址:

http://example.com/news/article/123/crime_is_down

而我需要得到的是这些:

Ouput #1: string

news/article

Ouput #2: an array

[array]
(
    0          => ['news'],
    1      => ['article']
)

是否有任何用于此的 php codeigniter 代码?

谢谢!

【问题讨论】:

  • 使用斜线爆炸/
  • $url=explode("/",$_SERVER[ 'REQUEST_URI' ]);
  • $url=explode("/",$_SERVER['REQUEST_URI']); print_r($url);回声 $url[1];回声 $url[2];
  • 第二个输出来自哪里?怎么样?

标签: php arrays codeigniter url


【解决方案1】:

它们似乎是控制器和方法,如果是这样,您可以简单地使用CI的功能,例如:

$this->router->fetch_class(); //gives you controller class name:: news
$this->router->fetch_method(); //gives you method name:: article

如果你使用 CI 2.0.2+,你可以这样做:

$router =& load_class('Router', 'core');
$router->fetch_class();
router->fetch_class();
router->fetch_method();

【讨论】:

    【解决方案2】:

    使用explode

    $url_string = http://example.com/news/article/123/crime_is_down
    
    $url=explode("/",$url_string); 
    
     print_r($url); 
     echo $url[1]; 
     echo $url[2];
    

    输出:

    array(
           [0]=>http://example.com)
           [1]=>news
           [2]=>article
           [3]=>123
           [4]=>crime_is_down
        )
    
        news
        article
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 2011-11-30
      • 2014-10-26
      • 1970-01-01
      • 2015-08-15
      相关资源
      最近更新 更多