|  | Posted by Rik on 06/12/65 11:48 
robert wrote:> "Gernot Frisch" <Me@Privacy.net> wrote in message
 > news:4dfvjuF19pargU1@individual.net...
 >> I don't know where to look for the syntax of that command.
 >>
 >>
 >> Here's the string I have:
 >> 0) Kung Fu : 1000n1) jack : 150n2) jim : 10n3) jonny : 0n
 >>
 >> It should split up in:
 >> array('Kung Fu' =>1000, 'jack'=>150, 'jim'=>10, 'jonny'=>0);
 >>
 >> That's what I have found somewhere:
 >> preg_match_all('/d+)s(.+)s:s(.+)/', $content, $tokens);
 >>
 >> But it gives:
 >> Warning: preg_match_all() [function.preg-match-all]: Compilation
 >> failed: unmatched parentheses at offset 2
 >
 > '\(\s*?([^:]*?)\s*?:\s*?(\d+)'
 >
 > something like that should get it...you didn't escape the first
 > parenth, nor did you escape *any* of the space characters (\s)...nor
 > would your expression have returned anything close to what your
 > description of results shows.
 
 Hmmz,  I almost suspect it has something to do with how he posts. I'm
 assuming that the n's are supposed to be \n's... Probably it should read:
 '/\d+\)\s(.+)\s:\s(.+)/', which would be somewhat workable.
 
 In that case this can be done faster without regexes:
 $array = array();
 $temp_array = explode("\n",$string);
 foreach($temp_array as $value){
 list($tkey, $tvalue) = explode(' : ',
 substr($value,strpos($value,')')+1));
 $array[trim($tkey)] = trim($tvalue);
 }
 
 Depending on how reliable the spaces are the trim() could be unneccesary.
 
 Grtz,
 --
 Rik Wasmus
  Navigation: [Reply to this message] |