Which delimiter was used?
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:
开发者_StackOverflow社区$val=explodeX(array("+","-","*","/"), $input);
Now, I need to return the right delimiter back in.
Thanks in advance.
All the delimeters you set in $delimiters
are used, as shown in this line:
while (isset($delimiters[$d_count]))
$d_count
is incremented at the bottom of the loop, $d_count++
, and $return_array
is returned after it loops through all your delimiters, seperating as much from $string
as 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:
$return_array[] = $new_return_array;
$delim = $delimiters[$d_count];
$return_array['delim'] = new Array();
$return_array['delim'][$delim][] = $new_return_array;
$d_count++;
This will store the words seperated by each delimiter in a child array under the delim
index of your returned result. Then all you would need to do to implode them back is something like this:
$string2 = '';
foreach ( $return_array as $delim => $word )
{
// not sure how to reverse your function at this moment, will get back with this part...
}
精彩评论