简体   繁体   English

无法连接x代码(\\ x) - PHP

[英]Can't concatenate x codes (\x) - PHP

I'm looking to use unpack(). 我想使用unpack()。

This works: 这有效:

$srbytes = "\x80\x3e\x00\x00";

$array1 = unpack("v",$srbytes); 

This does not: 这不是:

$num1 = "80"

$srbytes = "\x".$num1."\x3e\x00\x00";

$array1 = unpack("v",$srbytes);

or 要么

$srbytes = "\x"."80\x3e\x00\x00"; 
$array1 = unpack("v",$srbytes);

Printing this with echo shows ASCII chars with the first full string but, the concatenated ones shows text until it passes where it was concatenated. 使用echo打印它会显示带有第一个完整字符串的ASCII字符,但是连接的字符显示文本,直到它通过连接的位置。

Comparing a full string against a concatenated ones shows false, even though they should be the same? 将完整字符串与连接字符串进行比较显示为false,即使它们应该相同?

what is actually happening when I'm trying to concatenate 当我试图连接时实际发生了什么

The character expansion won't work, because at the point that you do "\\x" . "80" 字符扩展不起作用,因为你做"\\x" . "80" "\\x" . "80" PHP already has two string literals . "\\x" . "80" PHP已经有两个字符串文字 It can't be expected to figure that meant anything else but this. 它不能被预期的数字,意味着任何东西,但这个。

Instead of trying to concatenate a hexadecimal value for expansion, just concatenate the actual character, by converting the hexadecimal value to a base10 integer, and passing it to chr() , which converts it to an actual byte. 不是尝试连接十六进制值进行扩展,而是通过将十六进制值转换为base10整数并将其传递给chr()来连接实际字符, chr()将其转换为实际字节。

$str = "";
$num1 = "80";
$str .= chr(base_convert($num1, 16, 10));
var_dump($str);

Gives you 给你

string(1) "�"

When you actually look at the value of $srbytes in your example where you define it as a string literal "\\x80\\x3e\\x00\\x00" , what you get is var_dump("\\x80\\x3e\\x00\\x00") giving you string(4) " >" , because PHP double quoted strings offer additional character expansion such as expanding on escaped hexadecimal values into bytes. 当您在示例中查看$srbytes的值时,将其定义为字符串文字"\\x80\\x3e\\x00\\x00" ,您得到的是var_dump("\\x80\\x3e\\x00\\x00")给出的你是string(4) " >" ,因为PHP 双引号字符串提供额外的字符扩展,例如将转义的十六进制值扩展为字节。 However, var_dump("\\x"."80\\x3e\\x00\\x00") just gives you string(7) "\\x80>" , which is because the value "\\x" by itself is just a literal "\\x" as a string. 但是, var_dump("\\x"."80\\x3e\\x00\\x00")只给你string(7) "\\x80>" ,这是因为值"\\x"本身只是一个文字"\\x"作为一个字符串。 So they aren't the same values, no. 所以他们不是相同的价值观,不是。

If you want the 'literal' string use single quotes. 如果你想要'literal'字符串使用单引号。 Your issue is with escaped character sequences inside double quotes being evaluated. 您的问题是被评估的双引号内的转义字符序列。 Example: 例:

$srbytes = '\x'.'80\x3e\x00\x00'; 
echo $srbytes;
// \x80\x3e\x00\x00
var_dump($srbytes);
// string(16) "\x80\x3e\x00\x00" 

$srbytes = "\x"."80\x3e\x00\x00"; 
echo $srbytes;
// \x80>
var_dump($srbytes);
//string(7) "\x80>"

http://php.net/manual/en/language.types.string.php http://php.net/manual/en/language.types.string.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM