How to use Comma Separated String with switch statement - php

I have a string $str="1,2,4";
Also I have a switch statement
switch ($str_value) {
case '0':
$finalday='Sunday';
break;
case '1':
$finalday='Monday';
break;
case '2':
$finalday='Tuesday';
break;
case '3':
$finalday='Wednesday';
break;
case '4':
$finalday='Thursday';
break;
case '5':
$finalday='Friday';
break;
case '6':
$finalday='Saturday';
break;
default: $finalday='';
}
Using this statement from given string I want to show result
as Day is : Monday,Tuesday,Thursday
how can I do that?

Instead of switch case make an array of days
$days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Saturday'];
$str="1,2,4";
$val = [];
foreach(explode(',',$str) as $value){
$val[] = $days[$value];
}
echo implode(',',$val);//Monday,Tuesday,Thursday

Don't use a switch statement. Use preg_replace:
$final_day = $str_value;
$final_day = preg_replace('/0/', 'Sunday', $final_day);
// ...
$final_day = preg_replace('/6/', 'Saturday', $final_day);
And you have it.

If you really want to use the switch statement, explode the string into an array:
myDays = explode(",", $str);
Then loop over myDays with the switch statement in the loop.

You can use preg_replace_callback and pass a $days array to it.
That'll get the required digit(using the regex) and return day from the $days array.
Something like this -
$days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday","Friday","Saturday"]
preg_replace_callback('/\d+/',function($m)use($days){return $days[$m[0]];}, $str)
#OUTPUTS -
"Monday,Tuesday,Thursday"

Hope this will help:
<?php
$str='1,2,3';
$array=explode(',',$str);
$finalday='';
foreach($array as $a){
switch ($a) {
case '0':
$finalday.=',Sunday';
break;
case '1':
$finalday.=',Monday';
break;
case '2':
$finalday.=',Tuesday';
break;
case '3':
$finalday.=',Wednesday';
break;
case '4':
$finalday.=',Thursday';
break;
case '5':
$finalday.=',Friday';
break;
case '6':
$finalday.=',Saturday';
break;
default: $finalday='';
}
}
echo substr($finalday,1);

Related

PHP concatenate strings without whitespace

Let's say that
$result='first';
when I do:
$result.='second';
I got 'first second' instead of 'firstsecond'.
How to perfom concatenatation without adding this whitespace?
Edit:
That's the code. It should translate simple string to "pilots alphabet".
function converting($words){
echo $words;
global $result;
$result='';
for($x=0; $x<strlen($words); $x++){
switch($words{$x}){
case ' ':
$result.=' ';
break;
case 'A':
$result.='Alfa';
break;
case 'B':
$result.='Bravo';
break;
case 'C':
$result.='Charlie';
break;
case 'D':
$result.='Delta';
break;
case 'E':
$result.='Echo';
break;
case 'F':
$result.='Foxtrot';
break;
case 'G':
$result.='Golf';
break;
case 'H':
$result.='Hotel';
break;
case 'I':
$result.='India';
break;
case 'J':
$result.='Juliett';
break;
case 'K':
$result.='Kilo';
break;
case 'L':
$result.='Lima';
break;
case 'M':
$result.='Mike';
break;
case 'N':
$result.='November';
break;
case 'O':
$result.='Oscar';
break;
case 'P':
$result.='Papa';
break;
case 'Q':
$result.='Quebec';
break;
case 'R':
$result.='Romeo';
break;
case 'S':
$result.='Sierra';
break;
case 'T':
$result.='Tango';
break;
case 'U':
$result.='Uniform';
break;
case 'V':
$result.='Victor';
break;
case 'W':
$result.='Whiskey';
break;
case 'X':
$result.='Xray';
break;
case 'Y':
$result.='Yankee';
break;
case 'Z':
$result.='Zulu';
break;
}
}
return $result;
}
That switch also adds some cases that I have not defined. It is adding other characters to result, like '?' or '!' if they appear in 'words' string.
While I can't identify your problem (other posters are correct, your code is essentially right, the problem must be in the souce string), you may want to consider this somewhat more simple solution:
<?php
function phoneticAlphabetTranslation($word) {
$translations = [
' ' => ' ',
'a' => 'Alpha',
'b' => 'Beta',
'c' => 'Charlie',
];
return str_ireplace(array_keys($translations), array_values($translations), $word);
}
echo phoneticAlphabetTranslation('abAC');
each of the keys in $translations will be mapped to its value in the supplied string. Added benefit of being case-insensitive. If you don't desire case-insensitivity, just replace str_ireplace with str_replace.
You may already have invisible whitespace in your strings.
Try passing the strings through trim before adding to the $result variable
$result = trim('first');
$result .= trim('second');
echo $result;
Alternatively remove all whitespace after joining.
echo str_replace(" ", "", $result)

PHP Switch doesn't return correctly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
For my website I made a form, and one of the form values is for a month they choose. I have the month return with a value from 1-12, 1 being Jan and 12 being Dec, and I'm having the PHP code below interpret it.
switch ($_POST['month']) {
case '1':
$GLOBALS['month'] = 'Jan';
break;
case '2':
$GLOBALS['month'] = 'Feb';
break;
case '3':
$GLOBALS['month'] = 'March';
break;
case '4':
$GLOBALS['month'] = 'April';
break;
case '5':
$GLOBALS['month'] = 'May';
break;
case '6':
$GLOBALS['month'] = 'June';
break;
case '7':
$GLOBALS['month'] = 'July';
break;
case '8':
$GLOBALS['month'] = 'Aug';
break;
case '9':
$GLOBALS['month'] = 'Sept';
break;
case '10':
$GLOBALS['month'] = 'Oct';
break;
case '11':
$GLOBALS['month'] = 'Nov';
break;
case '12':
$GLOBALS['month'] = 'Dec';
break;
default:
$GLOBALS['month'] = '';
break;
};
echo 'month '.$month; //returns "month 12"
The issue is that $month always returns "12", no matter what the original value is (it doesn't even return "Dec", it just returns "12"). I can't seem to figure out what is wrong with the switch.
Simplified form
<form action="http://foo.com/foo-redirect/" method="POST" target="_blank">
<select name="month" required><option value="" disabled selected>Month</option><option value="1">January</option><option value="2">February</option><option value="3">March</option><option value="4">April</option><option value="5">May</option><option value="6">June</option><option value="7">July</option><option value="8">August</option><option value="9">September</option><option value="10">October</option><option value="11">November</option><option value="12">December</option></select>
<select name="day" required><option value="" disabled selected>Day</option><option value="12">1</option><option value="12">2</option><option value="12">3</option><option value="12">4</option><option value="12">5</option><option value="12">6</option><option value="12">7</option><option value="12">8</option><option value="12">9</option><option value="12">10</option><option value="12">11</option><option value="12">12</option><option value="12">13</option><option value="12">14</option><option value="12">15</option><option value="12">16</option><option value="12">17</option><option value="12">18</option><option value="12">19</option><option value="12">20</option><option value="12">21</option><option value="12">22</option><option value="12">23</option><option value="12">24</option><option value="12">25</option><option value="12">26</option><option value="12">27</option><option value="12">28</option><option value="12">29</option><option value="12">30</option><option value="12">31</option></select>,
<select name="year" required><option value="" disabled selected>Year</option><option value="2016">2016</option><option value="2017">2017</option></select>
</form>
I submitted the form as
Month: 1 (Jan)
Day: 5
Year: 2016
Update
It is actually a part of the omitted code that had the issue, thank you for the help.
I was incorrect, it actually appears to be a problem with WordPress (I use WordPress to make website changes easier). Though it says the version is PHP5.6, it lacks some features of PHP5.
$GLOBALS['month'] = '';
switch ($_POST['month']) {
case '1':
$GLOBALS['month'] = 'Jan';
break;
case '2':
$GLOBALS['month'] = 'Feb';
break;
case '3':
$GLOBALS['month'] = 'March';
break;
case '4':
$GLOBALS['month'] = 'April';
break;
case '5':
$GLOBALS['month'] = 'May';
break;
case '6':
$GLOBALS['month'] = 'June';
break;
case '7':
$GLOBALS['month'] = 'July';
break;
case '8':
$GLOBALS['month'] = 'Aug';
break;
case '9':
$GLOBALS['month'] = 'Sept';
break;
case '10':
$GLOBALS['month'] = 'Oct';
break;
case '11':
$GLOBALS['month'] = 'Nov';
break;
case '12':
$GLOBALS['month'] = 'Dec';
break;
default:
$GLOBALS['month'] = '';
break;
};
echo $GLOBALS['month'];
Returns correctly the month names.
Your error has to be somewhere later in the code if a print_r of $_POST['month'] at the very beginning of the file returns a value.
1) Check contain of $_POST data.
2) Checking form if $_POST data is missing something.
3) Checking the global var directly after the switch statement end.
4) If 1 and 3 return data, your error is somewhere later in the code.
After your question Update:
$month = $GLOBALS['month'];
echo $month;
Should work if you are not editing the value of $GLOBALS['month'] somewhere else.

My PHP code is not working properly

I´m trying to do some program which would transfer hexa do binary. Problem is in changing of A,B,C,..,F to 10,11,12,...,15 so i can work with them as with numbers. I made this function:
function odstran_pismena($pole)
{
$dlzka = count($pole);
for ($i = 0; $i< $dlzka; $i++)
switch ($pole[$i])
{
case 0: break;
case 1: break;
case 2: break;
case 3: break;
case 4: break;
case 5: break;
case 6: break;
case 7: break;
case 8: break;
case 9: break;
case ("A" || "a"): $pole[$i] = 10;
break;
case ("B" || "b"): $pole[$i] = 11;
break;
case ("C" || "c"): $pole[$i] = 12;
break;
case ("D" || "d"): $pole[$i] = 13;
break;
case ("E" || "e"): $pole[$i] = 14;
break;
case ("F" || "f"): $pole[$i] = 15;
break;
default: $pole[$i] = "ERROR";
break;
}
return $pole;
}
First i made array from string, and now i want to change letters to numbers.
I´m testing it with this string: $test = "AbCdEf2345";
I was expecting result 10 11 12 13 14 15 2 3 4 5 but all i have is 10 10 10 10 10 10 2 3 4 5
Am I doing some mystake?(Of course I am, but where?)
("A" || "a") evaluates to boolean value 'true', so all a to f will get caught by the case ("A" || "a" ) and result in 10.
Without using the hexdec() and with minimal change to your code:
function odstran_pismena($pole)
{
$dlzka = count($pole);
for ($i = 0; $i< $dlzka; $i++)
switch ($pole[$i])
{
case 0: break;
case 1: break;
case 2: break;
case 3: break;
case 4: break;
case 5: break;
case 6: break;
case 7: break;
case 8: break;
case 9: break;
case "A":
case "a": $pole[$i] = 10;
break;
case "B":
case "b": $pole[$i] = 11;
break;
case "C":
case "c": $pole[$i] = 12;
break;
case "D":
case "d": $pole[$i] = 13;
break;
case "E":
case "e": $pole[$i] = 14;
break;
case "F":
case "f": $pole[$i] = 15;
break;
default: $pole[$i] = "ERROR";
break;
}
return $pole;
}
When you have specified case ("A" || "a"): $pole[$i] = 10; it evaluates to true. So all your chars matched against true returns true. for 'case' conditions avoid using expressions. Use the static values your comparing such as
case "A":
case "a":
$pole[$i] = 10;
break;
An easy way to help you with that would be to use a strtolower:
switch(strtolower($pole[$i])) {
case "a" :...
break;
case "b": ...
break;
}
or simply, as the first line of your function:
$pole = strtolower($pole);
This way you won't have to bother with upper/lower casing.
You should use hexdec() and replace you whole for loop with:
for ($i = 0; $i< $dlzka; $i++)
{
$pole[$i] = hexdec($pole[$i]);
}
Note that you will receive a 0 for non-valid values so you might have to check for that separately if it can happen, using for example is_numeric on the original value.

PHP Switch with 2 values

is there a way to the php's SWITCH but with 2 values? Here's what I'm looking for
switch(a, b){
case 1,2: some code... ; break;
case 3,4: some code... ; break;
case 3,6: some code... ; break;
case 5,2: some code... ; break;
case 1,3: some code... ; break;
case 8,5: some code... ; break;
}
I know this won't work, so how would i do something along these lines?
You could use an array with 2 elements since == comparison checks the array values:
$a = 3;
$b = 6;
switch([$a, $b]){
case [1, 2]: echo '1'; break;
case [3, 4]: echo '2'; break;
case [3, 6]: echo '3'; break;
case [5, 2]: echo '4'; break;
case [1, 3]: echo '5'; break;
case [8, 5]: echo '6'; break;
}
Outputs 3.
You could use some string instead if it isn't heavy processing:
$variable= "1,2";
switch($variable){
case "1,2": some code... ; break;
case "3,4": some code... ; break;
case "3,6": some code... ; break;
case "5,2": some code... ; break;
case "1,3": some code... ; break;
case "8,5": some code... ; break;
}

Switch, same value for multiple case

switch ($i) {
case A:
$letter = 'first';
break;
case B:
$letter = 'first';
break;
case C:
$letter = 'first';
break;
case D:
$letter = 'second';
break;
default:
$letter = 'third';
}
Is there any way to shorten first three cases?
They have the same values inside.
switch ($i) {
case A:
case B:
case C:
$letter = 'first';
break;
case D:
$letter = 'second';
break;
default:
$letter = 'third';
}
Yep there is. If there's no break after a case, the code below the next case is executed too.
switch ($i) {
case A:
case B:
case C:
$letter = 'first';
break;
case D:
$letter = 'second';
break;
default:
$letter = 'third';
}

Categories