$binary = b'Binary string';
What consequences does it have to create a string as b
inary?
I couldn't find any hint abo开发者_JAVA百科ut that in the documentation. Just found this little curiosity while looking through the language_scanner.
This is a forward compatibility token for the never-to-be-released PHP version 6, which should have had native unicode support.
In PHP6, strings are unicode by default, and functions operate at the unicode character level on them. This "b" means "binary string", that is, a non unicode string, on which functions operate at the byte level.
This has no effect in PHP != 6, where all strings are binary.
Binary casting is available since 5.2.1 but will not take effect until 6.0 when unicode strings also take effect.
Which explains why this does nothing special right now for me on a server using 5.2.6:
<?php
$t = b"hey";
var_dump($t);
//string(3) "hey"
$s = (binary)"hey";
var_dump($s);
//string(3) "hey"
?>
Convert to string
$binary = preg_replace('/[[:^print:]]/', '', $binary);
精彩评论