I've got a string that could be in one of two forms:
prefix=ke开发者_如何学运维y=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.;-)
Edit:
Now I notice that you're getting the "if prefix should be there or not" from somewhere else, in which case my original solution is probably a bit less elegant. Instead, what I'd suggest is something like this:
$parts = explode('=', $str);
$key = array_shift($parts);
if($has_prefix) {
$key .= '=' . array_shift($parts);
}
$val = implode('=', $parts);
.
My original response:
$parts = array_reverse(explode('=', $str));
$val = array_shift($parts);
$key = implode('=', array_reverse($parts));
how about just
$parts = explode('=', $str);
$key = array_shift( $parts);
//additionally, shift off the second part of the key
if($split_on_second)
{
$key = $key . '=' . array_shift($parts);
}
//recombine any accidentally split parts of the value.
$val = implode($parts, "=");
Another variation
$explodeLimit = 2;
if($split_on_second)
{
$explodeLimit++;
}
$parts = explode('=', $str, $explodeLimit);
//the val is what's left at the end
$val = array_pop($parts);
//recombine a split key if necessary
$key = implode($parts, "=");
And haven't tested this, but seems like it could be one of those fun optimizations that make code accurate but unreadable...
$explodeLimit = 2;
//when split_on_second is true, it bumps the value up to 3
$parts = explode('=', $str, $explodeLimit + $split_on_second );
//the val is what's left at the end
$val = array_pop($parts);
//recombine a split key if necessary
$key = implode($parts, "=");
if ($prefix_exists) {
list($prefix, $str) = explode('=', $str, 2);
$prefix .= '=';
}
list($key, $val) = explode('=', $str, 2);
$key = $prefix . $key;
精彩评论