clever way to conditionally split this string?
- by sprugman
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.;-)