我有一个数据帧转换为二进制字符串。我需要从该字符串中提取特定的位。有人可以告诉我我该怎么做吗?
我有这个二进制字符串,并将此字符串拆分为数组。我可以访问每一位,但不能从中获取任何信息。
$ str =“ 11110000111100000001 101010101010 0010 101110111011 ”;
print_r(str_split($str));
我想从字符串中以粗体显示特定的位并打印出来。
$var=101010101010;
echo ($var);
$var2=101110111011;
echo ($var2);
这是所需的类型,但无法理解如何从字符串中获取。
答案 0 :(得分:0)
您可以使用preg_match
。这将找到您要查找的所有特定块并将它们列出在数组中。
$str="11110000111100000001 101010101010 0010 101110111011";
preg_match_all("/(101010101010)|(101110111011)/", $str, $matches);
var_dump($matches[0]); //0th index gives you all the matches
这将为您提供如下结果:
array(2) { [0]=> string(12) "101010101010" [1]=> string(12) "101110111011" }
然后,您可以遍历数组并根据需要打印每个值:
foreach ($matches[0] as $match) {
echo $match.PHP_EOL;
}
答案 1 :(得分:0)
如果知道所需块的位置和所需块的长度,则可以使用substr。
$str="111100001111000000011010101010100010101110111011";
$startPos = 20;
$length = 12;
$output=substr($str, $startPos, $length);
echo $output; // 101010101010