1
votes

I got big file with a format like this(its readed in one char at a time):

Lorem ipsum dolor | sit amet, | consectetur | adipiscing elit.

That is seperated by a "|" symbol.
I want to store these in an array. Like this:

0 => Lorem ipsum dolor
1 => Lorem ipsum dolor | sit amet
2 => Lorem ipsum dolor | sit amet, | consectetur
3 => Lorem ipsum dolor | sit amet, | consectetur | adipiscing elit.

Is there a way to this without needing to go through the entire line again to get them seperated ?

1
How do you plan to use them? Seems like rather than storing them with duplication like that, you might just explode the delimited string to a regular array and use the array indices to construct the strings in their accumulating form as above.Michael Berkowski

1 Answers

0
votes

The simplest way to do it to come to my mind is as follows

$array will contain what you need

$str = "Lorem ipsum dolor | sit amet, | consectetur | adipiscing elit.";
$array = array();
$cnt = 0;

while(true)
{
    $itms = explode( ' | ', $str, $cnt );
    if(count($itms)==0)
    {
        break;
    }
    else
    {
       $array[] = implode( ' | ', $itms );
    }
    $cnt--;
}

$array = array_reverse( $array );