URL 空间替换添加到 URL 中的 cauese 20


URL space replace cauese 20 added to the url

我的codeigniter控制器名称就像asd ASd Aldsds 98Hsds一样。我需要用-符号替换空格。为此我写了

$news_title = $this->uri->segment(1);
    $news_title = strtolower($news_title);
    $url = preg_replace("![^a-z0-9]+!i", "-", $news_title);

但上面的行输出$url为: asd%20asd%20aldsds%2098hsds如何从我的网址中删除这些%20。为什么会出现这些?

preg_replace之前使用 urldecode。

$news_title = urldecode($news_title);

似乎是...->uri->...将进行 URL 编码。网址不得包含空格。如果无法避免它们,则需要将它们转换为 %20 以使 url 有效。