phparraysstring

How can i transform a string in an array by two separators in php?


I have a string that i would like to explode into an array by two separators instead of one.

What is the most efficient way to do it?


Solution

  • You can turn this into a single explode() script.

    Do a str_replace() with any of the two separators you are dealing with, so now you are facing a string with only one separator. Let's say you have a string like "banana, apple ! pineapple, melon". If you do str_replace($str, '!', ',') you will have "banana, apple, pineapple, melon" and then you can do a explode($str, ',') and it will give you your array!