PHP奇怪的位运算符影响字符串

时间:2021-11-07 07:28:01

Update.. moved to a new question.

Okay, after reading PHP documentation it's clear now with those bitwise operators, but, huh, what is this?

在阅读PHP文档之后,这些位运算符就很清楚了,但是,这是什么?

#dump1
var_dump('two identical strings' | 'two identical strings'); // mind the |
// string(21) "two identical strings"

#dump2
var_dump('two identical strings' ^ 'two identical strings'); // mind the ^
// string(21) ""

Why #dump2 shows that length == 21, but 0 characters?

为什么#dump2显示长度= 21,而0字符?

When copied in Notepad++ there are no signs of characters inside the string, so, how come strlen > 0? - this confuses me, because Notepad++ can show some kind of bit-level (at least I think that those are bit-level, correct me if I'm wrong) characters, see picture: PHP奇怪的位运算符影响字符串

当在Notepad++中复制时,字符串中没有字符的标记,那么为什么strlen >是0呢?-这让我困惑,因为Notepad++可以显示某种位级(至少我认为那些是位级的,如果我错了请纠正)字符,见图:

This is actually result from:

这实际上是由于:

$string = 'i want you to compare me with an even longer string, that contains even more data and some HTML characters, like € ' ^ 'And I am going to add some HTML characters, like € again to this side and see what happens'; // mind the ^
var_dump(htmlentities($string)); // had to add htmlentities, otherwise &gt; (<) characters are added in this case, therefore messing up the output - Chrome force closes `<body>` then
// string(101) "(NA'TAOOGCP MI<<m-NC C IRLIHYRSAGTNHEI   RNAEAAOP81#?"

i'd love to see this #dump2 related question answered, thanks in advance!


While experimenting, I found out this:

在实验过程中,我发现:

echo 'one' | 'two'; 
// returns 'o'

echo 'one' | 'twoe';
// returns 'oe'

So, seeing that in those two lines it returns only letters which are in both strings, I was thinking it does some comparison or something:

所以,看到这两行只返回两个字符串中的字母,我想它会做一些比较

echo 'i want you to compare me' | 'compare me with this';    
#crazy-line // returns 'koqoveyou wotko}xise me'

While writing this, even stranger stuff happened. I copied that returned value, and after pasting it into post textarea, when pointer is positioned at the end of crazy-line, it is actually one "space" to the right not where it should be. When backspacing, it clears last character, but pointer is still one "space" to the right.

写这篇文章的时候,甚至发生了更奇怪的事情。我复制了那个返回的值,在粘贴到post textarea之后,当指针被放置在crazy-line的末尾时,它实际上是右边的一个“空格”,而不是它应该在的地方。当返回间隔时,它清除最后一个字符,但是指针仍然是右边的一个“空格”。

That lead me to copy this value inside Notepad++:
PHP奇怪的位运算符影响字符串
And, huh, as you can see there is a 'boxy' character within this string that doesn't show up inside browser (at least on mine, Chrome, latest). And yes, when this character is removed from that string (by backspacing), it returns back to normal - no more "space" to the right.

这让我在Notepad++ +中复制了这个值:而且,嗯,您可以看到,这个字符串中有一个“四四方方”的字符,它在浏览器中不会显示出来(至少在我的浏览器Chrome中是最新的)。是的,当这个字符从该字符串中移除(通过后退)时,它会返回到正常-没有更多的“空间”向右。

So, first, what is this | inside PHP, and why there is such a strange behavior?

首先,PHP中的|是什么,为什么会有如此奇怪的行为?

And what is this even stranger character, that looks like a box and doesn't show up in browser?

这个更奇怪的字符是什么,看起来像一个盒子,在浏览器中不显示?

I'm pretty damn curious why this is happening, so here is one more test with longer strings containing HTML entities:

我很好奇为什么会发生这种情况,所以这里还有一个包含HTML实体的长字符串测试:

$string = 'i want you to compare me with an even longer string, that contains even more data and some HTML characters, like &euro; ' | 'And I am going to add some HTML characters, like &euro; again to this side and see what happens';
var_dump($string);
// returns string(120) "inwaota}owo}ogcopave omwi||mmncmwwoc|o~wmrl{wing}r{augcontuonwhmweimorendaweealawomepxuo characters, like € "

Last value contains 7 of those 'boxy' characters.

最后一个值包含7个“四四四方方”字符。

3 个解决方案

#1


7  

That's a bitwise OR operator. It's behavior on strings is explained here: http://php.net/manual/en/language.operators.bitwise.php#example-107

这是位运算符。这里解释了字符串的行为:http://php.net/manual/en/language.operators.bitwise.php#example-107

<?php
echo 12 ^ 9; // Outputs '5'

echo "12" ^ "9"; // Outputs the Backspace character (ascii 8)
                 // ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8

echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0
                        // 'a' ^ 'e' = #4

echo 2 ^ "3"; // Outputs 1
              // 2 ^ ((int)"3") == 1

echo "2" ^ 3; // Outputs 1
              // ((int)"2") ^ 3 == 1
?>

#2


7  

its the bitwise OR operator.

它是位运算符。

If both the left-hand and right-hand parameters are strings, the bitwise operator will operate on the characters' ASCII values.

如果左边和右边的参数都是字符串,位操作符将对字符的ASCII值进行操作。

Source: http://php.net/manual/en/language.operators.bitwise.php

来源:http://php.net/manual/en/language.operators.bitwise.php

#3


6  

The pipe character | is used for bitwise inclusive OR comparisons:

该管字符|用于位的兼容或比较:

http://www.php.net/manual/en/language.operators.bitwise.php

http://www.php.net/manual/en/language.operators.bitwise.php

Here is a previous SO thread on how it handles strings:

这里有一个关于如何处理字符串的线程:

How to Bitwise compare a String

如何按位比较字符串

#1


7  

That's a bitwise OR operator. It's behavior on strings is explained here: http://php.net/manual/en/language.operators.bitwise.php#example-107

这是位运算符。这里解释了字符串的行为:http://php.net/manual/en/language.operators.bitwise.php#example-107

<?php
echo 12 ^ 9; // Outputs '5'

echo "12" ^ "9"; // Outputs the Backspace character (ascii 8)
                 // ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8

echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0
                        // 'a' ^ 'e' = #4

echo 2 ^ "3"; // Outputs 1
              // 2 ^ ((int)"3") == 1

echo "2" ^ 3; // Outputs 1
              // ((int)"2") ^ 3 == 1
?>

#2


7  

its the bitwise OR operator.

它是位运算符。

If both the left-hand and right-hand parameters are strings, the bitwise operator will operate on the characters' ASCII values.

如果左边和右边的参数都是字符串,位操作符将对字符的ASCII值进行操作。

Source: http://php.net/manual/en/language.operators.bitwise.php

来源:http://php.net/manual/en/language.operators.bitwise.php

#3


6  

The pipe character | is used for bitwise inclusive OR comparisons:

该管字符|用于位的兼容或比较:

http://www.php.net/manual/en/language.operators.bitwise.php

http://www.php.net/manual/en/language.operators.bitwise.php

Here is a previous SO thread on how it handles strings:

这里有一个关于如何处理字符串的线程:

How to Bitwise compare a String

如何按位比较字符串