Jump to content
php.lv forumi

Recommended Posts

Posted (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 by aywars
Posted

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+)/$

Posted

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.

Posted

diezgan neprātīgi ...

varbūt uzreiz tos parametrus liec masīvā ?

$var['x'] = 1;

$var['s'] = 1;

$var['c'] = 4;

$var['m'] = 1;

Posted (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 by v3rb0

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...