douyamitong57935 2018-09-24 21:53
浏览 33
已采纳

在PHP中避免相等和符号转换

I have a php variable containing some special characters, inside a Codeigniter 3 controller:

page_url = 'search=' . $expression . '&page';

In a template, I use this variable:

<a href="<?php echo $page_url; ?>"></a>

In the in the browser I see the characters mentioned above in this form:

posts/search?search%3Dharum%26page=2

The = sign turns to %3D, "&" to %26.

I tried page_url = urldecode($page_url); but it does not work.

How do I keep the original characters?

  • 写回答

2条回答 默认 最新

  • dslfq06464 2018-09-24 22:11
    关注

    Please use utf8 decode and try again

    echo utf8_decode(urldecode("search%3Dharum%26page=2"));
    

    Try this decode function.

    function decode($url)
    {
        $special = array(
            '%21' => '!', '\\' => '%5C', // so on you need to define.
        );
    
        foreach($special as $key => $value)
        {
            $result = str_replace($key, $value, $url);
        }
    
        return $result;
    }
    
    echo decode("search=%21");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部