【问题标题】:Codeigniter URI function url_title() how to useCodeigniter URI 函数 url_title() 如何使用
【发布时间】:2011-09-22 18:09:55
【问题描述】:

我正在使用 CodeIgniter 框架,我对如何从我的网址中删除 %20 感到困惑。下面是我的代码示例。 控制器 - 博客 方法 - 显示 属性 - 这是我的博客

public function show($blog= null)
    {
    // my attempt to set the uri segment
    $blogName = $this->uri->segment(3, url_title($blog));
    ... //other code
}

这不起作用,我很困惑在哪里实现 url_title('this is my blog') 函数,以便在页面加载时显示:

/blog/show/this-is-my-blog

我需要在 config/routes.php 文件中做些什么吗?

谢谢!

编辑:

好吧,我发现 url_title() 输出 this20is20my20blog 所以我现在有了这个:

      $blogUrl = str_replace("%20", "-", $blog);
    $this->uri->segment(3, $blogUrl);

但它仍然返回带有 %20 的 URL

【问题讨论】:

  • 如果不显示其他代码(例如,什么“返回”%20?),就不可能知道发生了什么。此外,您说您希望它显示“/blog/show/this-is-my-blog”,但您没有指定您浏览到的 URI 是什么,也没有指定是否传递 $ show() 函数的 blog 参数。

标签: php codeigniter url-rewriting


【解决方案1】:

您只需要使用原生 php 函数 urldecode 来删除在 url 中编码的任何字符。 URL 中的空格被编码为%20,所以不要使用 str_replace 来尝试

public function show($blog= null)
{
    // i'm not sure what url_title does so you might have to tweak this a little
    $blogName = $this->uri->segment(3, urldecode(url_title($blog)));
}

http://php.net/manual/en/function.urldecode.php

【讨论】:

    【解决方案2】:

    在执行 $this->uri->segment(); 之前尝试回显 url_title() 以确保它正确返回。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-09
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      相关资源
      最近更新 更多