本帖最后由 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;
}
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
相关文章
相关视频
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论