I am trying to split a binary string into an array of duplicate characters.
For example, an array 10001101divided by this function would be:
10001101
$arr[0] = '1'; $arr[1] = '000'; $arr[2] = '11'; $arr[3] = '0'; $arr[4] = '1';
(I tried to understand, but if you still do not understand, my question is the same as this one , but for PHP, not Python)
You can use preg_splitlike this:
preg_split
$in = "10001101"; $out = preg_split('/(.)(?!\1|$)\K/', $in); print_r($out);
Array ( [0] => 1 [1] => 000 [2] => 11 [3] => 0 [4] => 1 )
Regular expression:
(.)
(?!\1|$)
\K
. PHP 5.6.13, \K.
, , :
$out = preg_split('/(?<=(.))(?!\1|$)/', $in);
lookbehind , \K, .
<?php $s = '10001101'; preg_match_all('/((.)\2*)/',$s,$m); print_r($m[0]); /* Array ( [0] => 1 [1] => 000 [2] => 11 [3] => 0 [4] => 1 ) */ ?>
1 . ((.), $m[1]), (((.)\2*), $m[0]). preg_match_all . , . 'aabbccddee'. 0 1, [01] . .
$m[1]
((.)\2*)
$m[0]
'aabbccddee'
0
1
[01]
.
, $m , , , .. isset($m[0]), .
isset($m[0])
. , , , .
$chunks = array(); $index = 0; $chunks[$index] = $arr[0]; for($i = 1; $i < sizeof($arr) - 1; $i++) { if( $arr[$i] == $arr[$i-1] ) { $chunks[$index] .= $arr[$i]; } else { $index++; $chunks[$index] = $arr[$i]; } }
Source: https://habr.com/ru/post/1612120/More articles:Gulp VS2015.Net5 - asp.netСоздание свойства, заданного только определенным методом - enumsМожно ли сделать эту грамматику YACC однозначной? expr:...Django tests, transactions and angular protractor - pythonGoogle in the application Billing verification failed sometimes - android-payAndroid Espresso: "Тест не найден", "Процесс разбился", - androidHow to find and break a string with repeated characters? - pythonHow to override CTRL + Y in python script? - pythonevaluation of elasticsearch function, weight increase of “number of agreed conditions in a request” (coordination) - elasticsearchhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1612125/link-aspnet-identity-users-to-user-detail-table&usg=ALkJrhgL4LBAqKYJA39FIfyD4O42FM7ITQAll Articles