aywars Posted May 16, 2009 Report Share Posted May 16, 2009 (edited) Sveiki! Man te tāds jautājums.. Man ir mainīgais: $variable = 1s1c4m1 Kā lai šo mainīgo ar php palīdzību sadala 4 daļās? Doma apmēram tāda: $id = 1; $size = s1; $color = c4; $m = m1 Nu attiecīgi $size vienmēr sāksies ar burtu s, $color ar c u.t.t. Palīdziet lūdzu. Paldies! Edited May 16, 2009 by aywars Quote Link to comment Share on other sites More sharing options...
Klez Posted May 16, 2009 Report Share Posted May 16, 2009 ja tu zini ka pirmais vienmēr būs 1 simbols, otrais būs divi simboli (otrais un trešais) utt. tad paskaties šo fciju http://lv.php.net/substr Quote Link to comment Share on other sites More sharing options...
marcis Posted May 16, 2009 Report Share Posted May 16, 2009 Ja to visu zin, tad var iztikt arī bez substr - strings ir burtu/ciparu masīvs. $id = $variable[0]; $size = 's'.$variable[2]; ... Citādi jāmeklē ar regex, like: ^/(\d+)s(\d+)c(\d+)m(\d+)/$ Quote Link to comment Share on other sites More sharing options...
aywars Posted May 16, 2009 Author Report Share Posted May 16, 2009 Paldies par atbildēm.. Izdarīju bišķīt vienkāršāk.. Pārveidoju mainīgo ar zemsvītrām un izmantoju explode: $variable = 1_s1_c4_m1; $params = explode('_', $variable); $id = $params[0]; $color = $params[1]; u.t.t. Quote Link to comment Share on other sites More sharing options...
Klez Posted May 17, 2009 Report Share Posted May 17, 2009 diezgan neprātīgi ... varbūt uzreiz tos parametrus liec masīvā ? $var['x'] = 1; $var['s'] = 1; $var['c'] = 4; $var['m'] = 1; Quote Link to comment Share on other sites More sharing options...
v3rb0 Posted May 17, 2009 Report Share Posted May 17, 2009 (edited) $v = "1s1c4m1"; $z = array_map(create_function('$a,$b','return $b.$a;'), preg_split('/s|c|m/',$v), array('','s','c','m')); $id = current($z); $size = next($z); $color = next($z); $m = next($z); printf('id: %s; size:%s; color:%s; m:%s', $id, $size, $color, $m); Edited May 17, 2009 by v3rb0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.