2

First off, I have seen the following questions already:

And more. If I have an input like this:

A@B#C
a@b#c

For each line, I have the two delimiters @ and #, I would like to have them separated as an array this way:

Array
(
    [0] => A
    [1] => B
    [2] => C
)

Array
(
    [0] => a
    [1] => b
    [2] => c
)

I have the looping code this way:

foreach (explode(PHP_EOL, $input) as $line) {
    $line = explode("@", $line);
    $line[1] = explode("#", $line[1]);
}

I know what I am doing is wrong. The output I get is:

Array
(
    [0] => A
    [1] => Array
        (
            [0] => B
            [1] => C
        )
)

Array
(
    [0] => a
    [1] => Array
        (
            [0] => b
            [1] => c
        )
)

I am out of ideas to think anything else, so, can someone please guide me on how to split the string inside the same array? ps: I don't wanna use !

Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252

1 Answers1

2

My preferred solution would be to use preg_split, but since the question explicitly forbids the use of regex, how about just doing this:

$line = explode("#", str_replace("@", "#", $line));

So basically, just replace all occurrences of delimiter 1 with delimiter 2 and then split on delimiter 2.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156