I needed to split a string by different delimiters. So i found & used this code:
function explodeX($delimiters,$string)
{
$return_array = Array($string);
$d_count = 0;
while (isset($delimiters[$d_count]))
{
$new_return_array = Array();
foreach($return_array as $el_to_split)
{
$put_in_new_return_array = explode($delimiters[$d_count],$el_to_split);
foreach($put_in_new_return_array as $substr)
{
$new_return_array[] = $substr;
}
}
$return_array = $new_return_array;
$d_count++;
}
return $return_array;
}
It worked fine, but now, I need to reverse it and find, which delimeter it actually used.
I used this kind of line:
$val=explodeX(array("+","-","*","/"), $input);
Now, I need to return the right delimiter back in.
Thanks in advance.
All the delimeters you set in
$delimitersare used, as shown in this line:$d_countis incremented at the bottom of the loop,$d_count++, and$return_arrayis returned after it loops through all your delimiters, seperating as much from$stringas you have specified.To put the correct delimiter back in the position it was in before, you can’t, unless you modify what information you return in
$return_array. Something like this is what you’re looking for:This will store the words seperated by each delimiter in a child array under the
delimindex of your returned result. Then all you would need to do to implode them back is something like this: