clever way to conditionally split this string?

Posted by sprugman on Stack Overflow See other posts from Stack Overflow or by sprugman
Published on 2010-05-27T22:03:30Z Indexed on 2010/05/27 22:11 UTC
Read the original article Hit count: 141

Filed under:
|
|

I've got a string that could be in one of two forms:

prefix=key=value (which could have any characters, including '=')

or

key=value

So I need to split it either on the first or second equals sign, based on a boolean that gets set elsewhere. I'm doing this:

if ($split_on_second) {
    $parts = explode('=', $str, 3);
    $key = $parts[0] . '=' . $parts[1];
    $val = $parts[2];
} else {
    $parts = explode('=', $str, 2);
    $key = $parts[0];
    $val = $parts[1];
}

Which should work, but feels inelegant. Got any better ideas in php? (I imagine there's a regex-ninja way to do it, but I'm not a regex-ninja.;-)

© Stack Overflow or respective owner

Related posts about php

Related posts about parsing