I'm trying to explode a string using multiple delimiters (↑↑ , ↑ , ↓↓ , ↓
).
For example I have this intput string:
$string = "(2.8 , 3.1) → (↓↓2.4 , ↓3.0)";
I would like to convert it into an array like this (expected output):
Array
(
[0] => (2.8 , 3.1) → (
[1] => ↓↓
[2] => 2.4 ,
[3] => ↓
[4] => 3.0)
)
My best attempt prints me this (current output):
Array
(
[0] => (2.8 , 3.1) → (
[1] => ↓
[2] => ↓2.4 ,
[3] => ↓3.0)
)
This is my current code:
<?php
function multiexplode ($delimiters,$string) {
return explode(
$delimiters[0],
strtr(
$string,
array_combine(
array_slice($delimiters,1),
array_fill(0,count($delimiters)-1,array_shift($delimiters))
)
)
);
}
$delimiters = array('↑↑','↑','↓↓','↓');
$test = array('2up↑↑','1up↑','2down↓↓','1down↓');
$newDel = array('2up','1up','2down','1down');
$array = array();
$strings = array(
"(2.8 , 3.1) → (↓↓2.4 , ↓3.0)",
"(2.7 , 2.6) → (↑2.8 , ↑↑3.0)",
"(2.0 , 3.4) → (↑↑2.8 , ↓↓2.3)"
);
foreach($strings as $string){
foreach($test as $key => $reps){
$string = str_replace(
$delimiters[$key],
$reps,
$string
);
}
//echo $string;
$array[] = array_values(array_filter(multiexplode($newDel,$string)));
}
?>
I'm building it like that format, because I'm going to loop those values and print those inside a powerpoint and those delimeters(arrows) have different colors