user3220285
user3220285

Reputation:

Explode string that contains delimiter repeated successively

I'm looking how I can explode a string that where the delimiter is repeated successively but I didn't find the solution.

The string is: $text = "43##567#####152990#572##017"; and I want to get the numbers in an array().

I tried with explode() and putting the + as delimiter but it returns empty positions.

Upvotes: 1

Views: 578

Answers (3)

Giacomo1968
Giacomo1968

Reputation: 26066

Easy. Just use preg_split like this:

$data = "43##567#####152990#572##017";
$split_data = preg_split("/#?#+/is", $data, -1, null);

echo '<pre>';
print_r($split_data);
echo '</pre>';

And the results would be:

Array
(
    [0] => 43
    [1] => 567
    [2] => 152990
    [3] => 572
    [4] => 017
)

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53525

$res = preg_split('/\#+/', '43##567#####152990#572##017');
print_r($res);

OUTPUT:

Array
(
    [0] => 43
    [1] => 567
    [2] => 152990
    [3] => 572
    [4] => 017
)

Upvotes: 3

Flash Thunder
Flash Thunder

Reputation: 12036

<?php
    $text = "43##567#####152990#572##017";
    $data = array_filter(explode('#',$text));
    print_r($data);
?>

please note that it will remove zeroes aswell...

Upvotes: 2

Related Questions