How to split a string by repeated characters in PHP?

653

I'm trying to split a string with binary into an array of repeated characters.

For example, an array of10001101 split with this function would be:

    $arr[0] = '1';
    $arr[1] = '000';
    $arr[2] = '11';
    $arr[3] = '0';
    $arr[4] = '1';

(I tried to make myself clear, but if you still don't understand, my question is the same as this one but for PHP, not Python)

735

Answer

Solution:

{-code-1}

Matches repeated character sequences of 1 or more. The regex stores the subject character into the second capture group ((.), stored as$m[1]), while the first capture group contains the entire repeat sequence (((.)\2*), stored as$m[0]). With preg_match_all, it does this globally over the entire string. This can be applied for any string, e.g.'aabbccddee'. If you want to limit to just0 and1, then use[01] instead of. in the second capture group.

Keep in mind $m may be empty, to first check if the result exists, i.e.isset($m[0]), before you use it.

26

Answer

Solution:

I'm thinking something like this. The code id not tested, I wrote it directly in the comment, so it might have some errors, you can adjust it.

$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];
  }
}
379

Answer

Solution:

I wouldn't bother looking for the end-of-string in the pattern.

Most succinctly, capture the first occurring character then allow zero or more repetitions of the captured character, then restart the fullstring match with\K so that no characters are lost in the explosions.

Code: (Demo)

var_export(
    preg_split('~(.)\1*\K~', '10001101', 0, PREG_SPLIT_NO_EMPTY)
);

Output:

array (
  0 => '1',
  1 => '000',
  2 => '11',
  3 => '0',
  4 => '1',
)

If you don't care for regular expressions, here is a way of iterating through each character, comparing it to the previous one and conditionally concatenating repeated characters to a reference variable.

Code: (Demo) ...same result as first snippet

$array = [];
$lastChar = null;
foreach (str_split('10001101') as $char) {
    if ($char !== $lastChar) {
        unset($ref);
        $array[] = &$ref;
        $ref = $char;
        $lastChar = $char;
    } else {
        $ref .= $char;
    }
}
var_export($array);

People are also looking for solutions to the problem: linux - php shell_exec can't return "pidof php" on CentOS

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.