我正在使用CodeIgniter框架,我很困惑如何从我的url中删除%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
我需要在config/routes.php文件中做些什么吗?
谢谢!
编辑:我发现url_title()输出的是this20is20my20blog所以我现在有了这个:
$blogUrl = str_replace("%20", "-", $blog);
$this->uri->segment(3, $blogUrl);
但是它仍然返回带有%20的URL
您只需要使用本地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 在执行$this->uri->segment();
之前尝试回显url_title()
以确保其正确返回