2011-09-22 32 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();以確保它正確返回。