Re: pasring complex string question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, April 7, 2005 5:32 pm, Webmaster said:
> ## /T (KEY1)/V (VALUE1)|| ## /T (KEY2)/V (VALUE2)|| ## /V (VALUE3)/T
> (KEY3)||
>
> I know want to separete it in to keys and values and insert them into an
> array.
>
> Note that /T always shows that teh upcoming value in() is a Key and that
> /V
> always is a Value. And that the set can be flipped.

Question #1:
What happens when your KEY or VALUE contains "##", "/T", or "/V"
Depending on your answer to that, the best solution will change a bit.

In the short term:

$string = "## /T (KEY1)/V (VALUE1)|| ## /T (KEY2)/V (VALUE2)|| ## /V
(VALUE3)/T (KEY3)|| ";
$keyvalues = explode("##", $string);
$answer = array();
while (list(, $keyvalue) = each($keyvalues)){
  $keyvalue = trim($keyvalue);
  if (substr($keyvalue, 0, -2) != '||'){
    die("Unexpected keyvalue: $keyvalue");
  }
  $keyvalue = substr($keyvalue, 0, -2);
  preg_match("#/T(.*)(/V|$)#", $keyvalue, $key);
  $key = $key[1];
  preg_match("#/V(.*)(/T|$)#", $keyvalue, $value);
  $value = $value[1];
  $answer[$key] = $value;
}


-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux