Search code examples
phppreg-match

preg_match from beginning of array to backward slash


Could someone explain how to use php preg_match to break a string up into an array, from the beginning of the string to the first backward slash?

I have an array:

(
    [0] => s00473276\Soul To Squeeze\Red Hot Chili Peppers
    [1] => t00034422\Soul To Squeeze\Red Hot Chili Peppers
    [2] => 209676\Soul To Squeeze\Red Hot Chili Peppers
    [3] => s00473331\What Is Soul ?\Red Hot Chili Peppers
    [4] => 209672\Show Me Your Soul\Red Hot Chili Peppers
    [5] => t00034415\Show Me Your Soul\Red Hot Chili Peppers
    [6] => s00473268\Show Me Your Soul\Red Hot Chili Peppers
    [7] => s00473233\Out Of Range By Red Hot Chili Peppers\Red Hot Chili Peppers
    [8] => 209603\Get On Top\Red Hot Chili Peppers
    [9] => t00034374\I've Been Down\Red Hot Chili Peppers
)

And I want to create another array so I will be left with

[0] => s00473276
[1] => t00034422 etc...

Solution

  • $new_array = array();
    foreach($your_array as $element)
      list($new_array[]) = explode('\\', $element);
    
    print_r($new_array);