Encode the url including hyphen(-) and dot(.) in php

安稳与你 提交于 2019-11-29 03:46:15

Try this. Inside a function maybe if you are using it more than once...

$str = 'http://test.site.co/999999?lpp=1&p---x2=IjN';
$str = urlencode($str);
$str = str_replace('.', '%2E', $str);
$str = str_replace('-', '%2D', $str);
echo $str;

This will encode all characters that are not plain letters or numbers. You can still decode this with the standard urldecode or rawurldecode:

function urlencodeall($x) {
    $out = '';
    for ($i = 0; isset($x[$i]); $i++) {
        $c = $x[$i];
        if (!ctype_alnum($c)) $c = '%' . sprintf('%02X', ord($c));
        $out .= $c;
    }
    return $out;
}
benegan1991

Why don't you use rawurlencode

for example rawurlencode("http://test.site-raj.co/999999?lpp=1&px2=IjN")

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!