开发者

How do i encode all chars in a string

How do i encode all chars in a string.

Example : A = %32 // Or something like that

B = %33

I want to do this without hardcoding every char. And i als开发者_如何学Pythono want to be able to decode it again. Is there a php function for this?

Thanks.


If you really want to do this, you could do it with preg_replace_callback quite easily:

echo preg_replace_callback('/./', function($char) {
    return '%' . ord($char[0]);
}, 'this is probably an unnecessary step');
// %116%104%105%115%32%105%115%32%112%114%111%98%97%98%108%121%32%97%110%32%117%110%110%101%99%101%115%115%97%114%121%32%115%116%101%112

You could reverse it with the inverse, using chr:

echo preg_replace_callback('/%[^%]*/', function($seq) {
    return chr(substr($seq[0], 1));
}, '%116%104%105%115');
// this

However, this is almost certainly unnecessary for whatever you're doing...

See:

  • chr
  • ord
  • preg_replace_callback


try with:

urlencode($string);
urldecode($string);

http://php.net/manual/en/function.urlencode.php

http://php.net/manual/en/function.urldecode.php


Don't you just mean ord() and chr() ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜