请教php的url编码怎么编码字母和数字

ZMTX123 2018-01-01 11:21:04
urlencode 和 rawurlencode 都不会编码字母和数字, 比如字母D编码后应该是 %44, php有什么办法能把字母和数字都编码了
...全文
499 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZMTX123 2018-01-01
  • 打赏
  • 举报
回复
谁回复一下,我好结贴
ZMTX123 2018-01-01
  • 打赏
  • 举报
回复
刚才去php官网翻阅上面的函数,发现一个老外贴的代码解决了这个问题
I needed a function in PHP to do the same job as the complete escape function in Javascript. It took me some time not to find it. But findaly I decided to write my own code. So just to save time: 

<?php 
function fullescape($in) 
{ 
  $out = ''; 
  for ($i=0;$i<strlen($in);$i++) 
  { 
    $hex = dechex(ord($in[$i])); 
    if ($hex=='') 
       $out = $out.urlencode($in[$i]); 
    else 
       $out = $out .'%'.((strlen($hex)==1) ? ('0'.strtoupper($hex)):(strtoupper($hex))); 
  } 
  $out = str_replace('+','%20',$out); 
  $out = str_replace('_','%5F',$out); 
  $out = str_replace('.','%2E',$out); 
  $out = str_replace('-','%2D',$out); 
  return $out; 
} 
?> 

It can be fully decoded using the unscape function in Javascript.
红尘无爱 2018-01-01
  • 打赏
  • 举报
回复
你这编码的目的不就是混淆视听吗。直接找个 加解密的方法不也一样吗
xuzuning 2018-01-01
  • 打赏
  • 举报
回复
不至于吧?
$s = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+ ';
$r = '%' . join('%', array_map('dechex', unpack('C*', $s)));
echo $r;
%30%31%32%33%34%35%36%37%38%39%61%62%63%64%65%66%67%68%69%6a%6b%6c%6d%6e%6f%70%71%72%73%74%75%76%77%78%79%7a%41%42%43%44%45%46%47%48%49%4a%4b%4c%4d%4e%4f%50%51%52%53%54%55%56%57%58%59%5a%7e%21%40%23%24%25%5e%26%2a%28%29%5f%2b%20

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧