Search code examples
phpexplode

Split string only on first occurrence of a delimiter


I need some help, is it possible to explode like this? I have a string 45-test-sample, is it possible to have

["45", "test-sample"]

How can it be done?


Solution

  • print_r(explode('-', $string, 2));    // take some help from the limit parameter
    

    According to the PHP manual for **[explode][1]**

    If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

    Output:

    Array
    (
        [0] => 45
        [1] => test-sample
    )