日期:2014-05-17  浏览次数:20501 次

真诚求一个自定义,谢谢
本帖最后由 bonlog 于 2012-12-07 09:08:21 编辑


当输入
Monday/Tuesday/Wednesday/Thursday/Friday/Saturday/Sunday
输出
1,2,3,4,5,6,7

为了便于扩展,最好使用数组。

------解决方案--------------------
function foo($s) {
  $dict = array_flip(explode(',', '/,monday,tuesday,wednesday,thursday,friday,saturday,sunday'));
  $dict['/'] = ',';
  return strtr(strtolower($s), $dict);
}

echo foo('Monday/Tuesday/Wednesday/Thursday/Friday/Saturday/Sunday');


1,2,3,4,5,6,7

------解决方案--------------------
空格和'/'均作处理


function foo($s) {
$array=array('monday'=>1,
'tuesday'=>2,
'wednesday'=>3,
'thursday'=>4,
'friday'=>5,
'saturday'=>6,
'sunday'=>7,
'/'=>',',' '=>',');
  return strtr(strtolower($s), $array);

echo foo('Monday Tuesday Wednesday/Thursday/Friday/Saturday/Sunday');


------解决方案--------------------

function foo($s) {
    $array= array('monday'   => 1,
                 'tuesday'  => 2,
                 'wednesday'=> 3,
                 'thursday' => 4,
                 'friday'   => 5,
                 'saturday' => 6,
                 'sunday'   => 7,
                );

    $sa = preg_split('/\W/',strtolower($s));
    $arr = array();
    foreach ($sa as $v) {
        if(is_numeric($v)) {
            $t = array_values($array);
            $arr[] = $t[$v-1];
        }
        else if (isset($array[$v])) {
            $arr[] = $array[$v];
        }
        else {
            foreach ($array as $k1 => $v1 )  {
                if ( substr($k1, 0, 4) == $v ) {
                    $arr[] = $v1; break;
  &nb