php - regex to parse a string with known string seprarators -


i'm trying split concatenated string of key1value1key2value2 problem can't know in order are

$k = preg_split("/(name|age|sex)/", "namejohnage27sexm"); var_dump($k); $k = preg_split("/(sex|name|age)/", "age27sexm"); var_dump($k);  

so can't know if age or name 1st or 2nd index of $k, don't know if "name" key in string, there can limited set of key

how do?

edit: solved this, tx mario

for ($i=1, $n=count($k)-1; $i<$n; $i+=2) {     $s[$k[$i]] = $k[$i+1]; } var_dump($s); 

this clumsy pattern return key-value list:

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

thus preg_match using above on "namejohnage27sexm" should return array

["name", "john", "age", "27", "sex", "man"] 

this makes possible create array ["name" => "john", ...] iterating on elements above.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -