PHP string in hex

I have a line like this:

[0-9A-Za-z\+/=]* 

How can I convert as follows:

 "\133\x30\55\x39\101\x2d\132\x61\55\x7a\134\x2b\57\x3d\135\x2a" 

Is there any function for this?

+6
source share
2 answers
 function strtohex($string) { $string = str_split($string); foreach($string as &$char) $char = "\x".dechex(ord($char)); return implode('',$string); } print strtohex("[0-9A-Za-z\+/=]*"); 

The above code will give you

 \x5b\x30\x2d\x39\x41\x2d\x5a\x61\x2d\x7a\x5c\x2b\x2f\x3d\x5d\x2a 

I know this is not like the result you expect, but it is not like a line in hex.

+11
source

If you want to obfuscate a string, use something like the @Kristians approach. And you can alternate between two encoding methods, for example:

  $char = (++$i%2) ? "\x".dechex(ord($char)) : "\\".decoct(ord($char)); 
+2
source

Source: https://habr.com/ru/post/903928/


All Articles