Jump to content
php.lv forumi

PHP.. sadalīt mainīgo pa daļām


aywars

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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
Link to comment
Share on other sites

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...