Reply to Re: pasring complex string question

Your name:

Reply:


Posted by Eli on 04/08/05 07:39

Eli wrote:
> Webmaster wrote:
>
>> Hello,
>> i have a string looking like this.
>>
>>
>>
>> ## /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.
>>
>>
>>
>> Thank you very much for helping.
>>
>> Mirco Blitz
>>
>>
>
> It seems you complex yourself too much.. ;)
>
> Try using regexp with the 'e' modifier:
>
> <?php
>
> $data="## /T (KEY1)/V (VALUE1)|| ## /T (KEY2)/V (VALUE2)|| ## /V
> (VALUE3)/T (KEY3)||";
>
> $data_array=array();
> preg_replace(
> array(
> "/\#\#\s*/T\s*\(([^\)]*)\)\s*\V\s*\(([^\)]*)\)\s*\|\|/e",
> "/\#\#\s*/V\s*\(([^\)]*)\)\s*\T\s*\(([^\)]*)\)\s*\|\|/e"
> ),
> array(
> "\$data_array['\\1']='\\2';",
> "\$data_array['\\2']='\\1';"
> ),
> $data
> );
>
> print_r($data_array); //see if you get it as expected
>
> ?>
>
> I believe there's an easier way to store your data as strings (like
> serialize), so why not using them?!
> see alse: http://www.php.net/serialize

Sorry... a little correction:

<?php

$data="## /T (KEY1)/V (VALUE1)|| ## /T (KEY2)/V (VALUE2)|| ## /V
(VALUE3)/T (KEY3)||";

$data_array=array();
preg_replace(
array(
"/\#\#\s*\/T\s*\(([^\)]*)\)\s*\/V\s*\(([^\)]*)\)\s*\|\|/e",
"/\#\#\s*\/V\s*\(([^\)]*)\)\s*\/T\s*\(([^\)]*)\)\s*\|\|/e"
),
array(
"\$data_array['\\1']='\\2';",
"\$data_array['\\2']='\\1';"
),
$data
);

print_r($data_array); //see if you get it as expected

?>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация