Search code examples
phpregextext-parsingtext-extractionpreg-split

preg_split and multiple delimiters


let me start by saying the first number before the first - will be the ID I need to extract. from the first - to the first / will be the 'name' I need to extract. Everything after that I do not care for.

Test String:

1-gc-communications/edit/profile_picture

Expected Output:

Array ( [0] => 1 [1] => gc-communications [2] => /edit/profile_picture )

The best I could come up with was the following patterns (along with their results - with a limit of 3)

Pattern: /-|edit\/profile_picture/
Result: Array ( [0] => 1 [1] => gc [2] => communications/edit/profile_picture )

^ This one is flawed because it does both dashes.

Pattern: /~-~|edit\/profile_picture/
Result: Array ( [0] => 1-gc-communications/ [1] => )

^ major fail.

I know I can do a 2-element limit and just break on the first / and then do a preg_split on the result array, but I would love a way to make this work with one line.

If this is a no-go I am open to other "one liner" solutions.


Solution

  • Try this one

    $str = '1-gc-communications/edit/profile_picture';
    $match = preg_split('#([^-]+)-([^/]+)/(.*)#', $str, 0, PREG_SPLIT_DELIM_CAPTURE);    
    print_r($match);
    

    return like as

     array (
        0 => '',
        1 => '1',
        2 => 'gc-communications',
        3 => 'edit/profile_picture',
        4 => '',
      )