Sincere customization, thank you

Source: Internet
Author: User
Sincerely ask for a custom one. thanks for this post, which was last edited by bonlog in 2012-12-0709: 08: 21. when MondayTuesdayWednesdayThursdayFridaySaturdaySunday is input, the output is, and 7, it is best to use arrays. ------ Solution ------------ sincerely ask for a custom one. thank you.

This post was last edited by bonlog at 09:08:21, January 7 ,.



When you enter
Monday/Tuesday/Wednesday/Thursday/Friday/Saturday/Sunday
Output
1, 2, 3, 4, 5, 6, 7

To facilitate expansion, it is best to use arrays.

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

------ Solution --------------------
Space and '/' are processed


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');


------ Solution --------------------

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.