2011-09-22 103 views
0

我使用CodeIgniter框架,我很困惑如何从我的网址中移除%20。以下是我的代码示例。 控制器 - 博客 方法 - 显示 属性 - 这是我的博客Codeigniter的URI函数url_title()如何使用

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

这并不工作,我很困惑,我实现url_title(“这是我的博客”)功能,使页面加载它显示:

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

做我需要做的事情在config/routes.php文件的文件?

谢谢!

编辑:

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

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

,但它仍然%返回的URL 20

+0

没有显示其它代码(例如,什么是“回报”的%20的?),它是不可能知道发生了什么。另外,你说你想让它显示“/ blog/show/this-is-my-blog”,但是你没有指定你浏览的URI是什么,也不是你是否通过$博客参数show()函数。 –

回答

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

尝试呼应url_title()之前你做$this->uri->segment();以确保它正确返回。