PHP字符串到十六进制 [英] PHP string to hex

查看:169
本文介绍了PHP字符串到十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串:

  [0-9A-Za-z\ + / =] * $如何转换为以下形式:


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

这有什么功能吗?

解决方案

  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\ + / =] *);

上述代码会给你

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

我知道它看起来不像输出expect,但是似乎不是字符串到十六进制。


I have a string like that:

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

How can I converted in the following form:

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

Is there any function for that ?

解决方案

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'm aware that it doesn't look like the output you expect, but that doesn't seem to be string to hex at all.

这篇关于PHP字符串到十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆