1

I'm trying to split a concatenated string of key1value1key2value2 The problem is I can't know in which order they are

$k = preg_split("/(name|age|sex)/", "nameJohnage27sexM");
var_dump($k);
$k = preg_split("/(sex|name|age)/", "age27sexM");
var_dump($k); 

So I can't know if the age or name will be 1st or 2nd index of $k, don't even know also if "name" key is in the string, there can be a limited set of key

How to do?

edit: solved like this, tx mario

for ($i=1, $n=count($k)-1; $i<$n; $i+=2) {
    $s[$k[$i]] = $k[$i+1];
}
var_dump($s);
  • 3
    possible duplicate of [Is there way to keep delimiter while using php explode or other similar functions?](http://stackoverflow.com/questions/2938137/is-there-way-to-keep-delimiter-while-using-php-explode-or-other-similar-function) – mario Jun 04 '12 at 22:03
  • can I explode with a set of delimiter strings and not just one string?, –  Jun 04 '12 at 22:05
  • yes sorry u'r right, capturing the delimter is good thx –  Jun 04 '12 at 22:08
  • 2
    This is just a ridiculous thing to do. What if someones name is Sage? – Galen Jun 04 '12 at 22:08
  • If you have any control of how that data is generated for you, I highly suggest you leverage that and make it more readable by a machine. You're going to run into issues when your values match part of your keys. – SamT Jun 04 '12 at 22:10
  • that's right Galen, I'v chosen the delimiter carefully, they are not name, age and sex –  Jun 04 '12 at 22:11
  • @Sam it comes from a key of a DHT database, we could put delimiters such as name(John)age(27)..but I don't expect the case of Galen can happen –  Jun 04 '12 at 22:13
  • Seems odd to parse a string if you can handle database. – Gilles Quénot Jun 04 '12 at 22:14
  • I don't want to do another read query, normally this parsing will not fail –  Jun 04 '12 at 22:16
  • @ca11111 If you can make it output `name(John)age(27)`, creating the regexp becomes a whole lot easier. – SamT Jun 04 '12 at 22:22
  • yes I know http://stackoverflow.com/questions/9741641/simple-js-regex-to-extract-word-between-parenthesis –  Jun 04 '12 at 22:24

1 Answers1

1

This somewhat clumsy pattern will return a key-value list:

/(?:(name|age|sex)(.+?(?=(?:name|age|sex|\z))))/g

Thus preg_match using the above on "nameJohnage27sexM" should return the array

["name", "John", "age", "27", "sex", "MAN"]

This makes it possible to create the array ["name" => "John", ...] by iterating over the elements above.

mzedeler
  • 4,177
  • 4
  • 28
  • 41
  • By the way - as the comments above indicate, you're bound for problems when not using any real delimiters here. Thus "John Sage" can't be serialized to this format. – mzedeler Jun 04 '12 at 22:34
  • nice thx, for keys they start with "$" for example making a conflict rare enough –  Jun 04 '12 at 22:40
  • In general, the greediness modifier can cause problems, so `/(?:(name|age|sex)((?:(?!name|age|sex).)*)/g` is more reliable. It's far more conventional too. – ikegami Jun 04 '12 at 22:51
  • Thanks for the comments. I was surprised how hard it was to write a regex for this type of problem. – mzedeler Jun 06 '12 at 20:39