本帖最后由 bonlog 于 2012-12-07 09:08:21 编辑
当输入
Monday/Tuesday/Wednesday/Thursday/Friday/Saturday/Sunday
输出
1,2,3,4,5,6,7
为了便于扩展,最好使用数组。
------解决方案--------------------function foo($s) {<br /> $dict = array_flip(explode(',', '/,monday,tuesday,wednesday,thursday,friday,saturday,sunday'));<br /> $dict['/'] = ',';<br /> return strtr(strtolower($s), $dict);<br />}<br /><br />echo foo('Monday/Tuesday/Wednesday/Thursday/Friday/Saturday/Sunday');<br />
Copier après la connexion
1,2,3,4,5,6,7
------解决方案--------------------空格和'/'均作处理
<br />function foo($s) {<br /> $array=array('monday'=>1,<br /> 'tuesday'=>2,<br /> 'wednesday'=>3,<br /> 'thursday'=>4,<br /> 'friday'=>5,<br /> 'saturday'=>6,<br /> 'sunday'=>7,<br /> '/'=>',',' '=>',');<br /> return strtr(strtolower($s), $array);<br />} <br />echo foo('Monday Tuesday Wednesday/Thursday/Friday/Saturday/Sunday');<br />
Copier après la connexion
------解决方案--------------------<br>function foo($s) {<br> $array= array('monday' => 1,<br> 'tuesday' => 2,<br> 'wednesday'=> 3,<br> 'thursday' => 4,<br> 'friday' => 5,<br> 'saturday' => 6,<br> 'sunday' => 7,<br> );<br><br> $sa = preg_split('/\W/',strtolower($s));<br> $arr = array();<br> foreach ($sa as $v) {<br> if(is_numeric($v)) {<br> $t = array_values($array);<br> $arr[] = $t[$v-1];<br> }<br> else if (isset($array[$v])) {<br> $arr[] = $array[$v];<br> }<br> else {<br> foreach ($array as $k1 => $v1 ) {<br> if ( substr($k1, 0, 4) == $v ) {<br> $arr[] = $v1; break;<br> }<div class="clear">
</div>
Copier après la connexion