[英]Regex Binary Pattern Search in PHP
I am trying to find and replace binary values in strings:我正在尝试查找和替换字符串中的二进制值:
$str = '('.chr(0x00).chr(0x91).')' ;
$str = preg_replace("/\x00\x09/",'-',$str) ;
But I am getting "Warning: preg_replace(): Null byte in regex" error message.但我收到“警告:preg_replace():正则表达式中的空字节”错误消息。
How to work on binary values in Regex/PHP?如何在 Regex/PHP 中处理二进制值?
It is because you are using double quotes "
around your regex pattern, which make the php engine parse the chars \\x00
and \\x09
.这是因为您在正则表达式模式周围使用双引号
"
,这使 php 引擎解析字符\\x00
和\\x09
。
If you use single quotes instead, it will work:如果您改用单引号,它将起作用:
$str = '(' . chr(0x00) . chr(0x91) . ')' ;
$str = preg_replace('/\x00\x09/', '-', $str) ;
But your regex seems also not to be correct, if I understood your question correctly.但是,如果我正确理解您的问题,您的正则表达式似乎也不正确。 If you want to replace the chars
\\x00
and \\x91
with a dash -
, you must put them into brackets []
:如果您想用破折号
-
替换字符\\x00
和\\x91
,则必须将它们放入方括号[]
:
$str = preg_replace('/[\x00\x91]/', '-', $str) ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.