Home > Backend Development > PHP Tutorial > 真诚自定义,多谢

真诚自定义,多谢

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 12:58:59
Original
878 people have browsed it

真诚求一个自定义,谢谢

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


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 />
Copy after login


------解决方案--------------------
<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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template