Colon separated values in database
I have database result开发者_开发百科s in the following format:
s:6:hobbit
the s is "string" and the value is the count of characters in the third section. What is this called? Colon separated values? Is the only way to deal with it, in PHP, through regex?
Specifically, I'm trying to get the value hobbit
. But obviously when I access the db to get anything I get the whole value s:6:hobbit
. Am I doomed to using regex to work with results like this?
Looks like the output from serialize()
. (Hint: unserialize()
*)
Alternatively, you can explode()
on :
with limit=3
. This is important as the string itself (everything from the second :
forward) may contain colons.
Also, why are these values in the DB in the first place?
*) EDIT It seems that serialize()
would have encoded the string as s:6:"hobbit"
(quoted) so the explode()
could prove to be the way to do this.
list($tmp, $tmp, $string) = explode(':', $dbString);
echo $string;
Demo: http://codepad.org/z91CztFm
精彩评论