Codeigniter URI函数url_title()如何使用

时间:2011-09-22 18:09:55

标签: php codeigniter url-rewriting

我正在使用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('这是我的博客')功能,以便在页面加载时显示:

/博客/显示/这-是-MY-博客

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

谢谢你!

编辑:

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

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

但仍会返回%20

的网址

2 个答案:

答案 0 :(得分:3)

您只需要使用本机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

答案 1 :(得分:1)

尝试在url_title()之前回复$this->uri->segment();以确保其正确返回。