Reputation:
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
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
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
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