【问题标题】:Print / Explode PHP string with slashes用斜杠打印/分解 PHP 字符串
【发布时间】:2018-07-19 17:53:41
【问题描述】:

我将以下 IP 地址编码为字符串:

"\x7F\0\0\x01"

虽然我可以用我的眼睛理解它是如何编码的(八位字节、斜杠、八位字节...),但我无法在 PHP 中进行操作。

我尝试使用 \ (或 double )作为分隔符来拆分(爆炸),但没有运气。

>>> $d['_ip']
=> "\x7F\0\0\x01"
>>> $ip = explode("\\", $d['_ip'])
=> [
    "\x7F\0\0\x01",
    ]

当我尝试回显时,它不打印。

>>> echo $d['_ip']
⏎
>>> 

我需要将每个八位字节作为字符串。

【问题讨论】:

    标签: php string split hex explode


    【解决方案1】:

    我想您发送的字符串来自 Javascript 或 JSON。文字字符“\”、“x”、“7”等不是发送的内容。该字符串表示的是四个单独的字节,每个字节从 0 到 255。

    试试这个:

    $ip = str_split($d['_ip']);   // Break the string into an array of individual bytes
    $ip = array_map('ord', $ip);  // Map those bytes to their integer equivalents via the `ord()` function
    $ip = implode('.', $ip);      // Cast the bytes back to strings and connect with dots
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多