首頁 >後端開發 >php教程 >真诚自定义,多谢

真诚自定义,多谢

WBOY
WBOY原創
2016-06-13 11:05:49935瀏覽

真诚求一个自定义,谢谢

本帖最后由 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 />


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


------解决方案--------------------
<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>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn