Pieduriens Posted December 3, 2014 Report Share Posted December 3, 2014 (edited) Labdien. Saskāros ar stringa formatēšanas problēmām..(līkām rokām :) ) Ir: multiselect forma kurā stāv: 30ml. 100ml, 500ml, 1pcs, 5pcs, 100pcs: taču jāizvada kaut kas līdzīgs = 30/100/500ml/1/5/100pcs, (šeit selektotas visas vērtības). Ar str_replace varētu no stringa aizvākt visus "ml" un "pcs". $string = "30ml/100ml/500ml/1pcs/5pcs/100pcs"; $remove = array("ml","pcs"); $new = str_replace($remove, "", $string); Tikai pašam pēdējam jāpiekabina klāt vai nu "ml" vai "pcs". $goal = "30/100/500ml/1/5/100pcs"; Ir kādas idejas? Pateicos. Edited December 3, 2014 by Pieduriens Quote Link to comment Share on other sites More sharing options...
Kavacky Posted December 3, 2014 Report Share Posted December 3, 2014 Ko? Quote Link to comment Share on other sites More sharing options...
briedis Posted December 3, 2014 Report Share Posted December 3, 2014 echo preg_replace('#(\d+)[^/]+/#', '$1/', '30ml/100ml/500ml/1pcs/5pcs/100pcs'); Quote Link to comment Share on other sites More sharing options...
e-remit Posted December 3, 2014 Report Share Posted December 3, 2014 $string = "30ml/100ml/500ml/1pcs/5pcs/100pcs"; $rev_arr = array_reverse(explode("/", $string)); $used = array(); array_walk($rev_arr, function(&$v, $k, &$used) { $match = null; preg_match('/([0-9]+)([^0-9]+)/', $v, $match); $str = isset($match[2]) ? $match[2] : ""; if (in_array($str, $used)) { $v = isset($match[1]) ? $match[1] : ""; } else { $used[] = $str; } }, $used); $new_string = implode("/", array_reverse($rev_arr)); Quote Link to comment Share on other sites More sharing options...
Pieduriens Posted December 3, 2014 Author Report Share Posted December 3, 2014 echo preg_replace('#(\d+)[^/]+/#', '$1/', '30ml/100ml/500ml/1pcs/5pcs/100pcs'); Strādā tikai ja ir izmantots vai nu ml vai pc, kopā ne. Quote Link to comment Share on other sites More sharing options...
Roze Posted December 3, 2014 Report Share Posted December 3, 2014 Droši vien var smukāk, bet ienāca prātā kaut kas šāds: $string = "30ml/100ml/500ml/1pcs/5pcs/100pcs"; echo preg_replace('/ml/','',preg_replace('/pcs/','',$string,substr_count($string,'pcs')-1),substr_count($string,'ml')-1); Quote Link to comment Share on other sites More sharing options...
Roze Posted December 3, 2014 Report Share Posted December 3, 2014 Pa smuko. Quote Link to comment Share on other sites More sharing options...
jurchiks Posted December 3, 2014 Report Share Posted December 3, 2014 Find last pos of "ml", remove all "ml" before it. Same with "pcs". Quote Link to comment Share on other sites More sharing options...
Pieduriens Posted December 3, 2014 Author Report Share Posted December 3, 2014 $s="30ml/100ml/500ml/1pcs/5pcs/100pcs"; echo preg_replace('/([a-z]+)(?=.*\1)/','',$s); Kā ar šo? Un aiziet: kuram īsāk :) Quote Link to comment Share on other sites More sharing options...
jurchiks Posted December 7, 2014 Report Share Posted December 7, 2014 Īsāk ne vienmēr ir labāk, it īpaši, ja runa iet par regex. Quote Link to comment Share on other sites More sharing options...
codez Posted December 7, 2014 Report Share Posted December 7, 2014 bet biežāk ir labāk, nekā nav. Quote Link to comment Share on other sites More sharing options...
e-remit Posted December 7, 2014 Report Share Posted December 7, 2014 un codez neuzrakstīja, kā tas izskatītos Scalā?.. Quote Link to comment Share on other sites More sharing options...
jurchiks Posted December 7, 2014 Report Share Posted December 7, 2014 (edited) Es teiktu 50/50. Izņemot gadījumus, kur vnk kāds nezin kādu funkciju, kas visu kodu izdara 10 characteros. Edited December 7, 2014 by jurchiks 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.