// These are my Variables
$a = "a";
$b = "b";
$c = "c";
//My Post Form Data
$post = $_POST['name'];
//My Statements
if (isset($_POST['name']) && preg_match("/\b($a)\b/", $post )) {
echo '64';
}
if (isset($_POST['name']) && preg_match("/\b($b)\b/", $post )) {
echo '67';
}
if (isset($_POST['name']) && preg_match("/\b($c)\b/", $post )) {
echo '66';
}
The problem is I want abc
to be in form order
and to print more then once. So if I enter cba
I want it to print 666764
.
If I send my form input as cbaa
I want my input to be 66676464
. currently it would be posting as this 646766
!
edit: muhammads worked!
You can simply iterate from string
$statements = [
'a' = 64,
'b' = 67,
'c' = 66
];
$input = isset($_POST['name']) ? $_POST['name'] : null;
$output = null;
for($x = 0; $x < strlen($input); $x++) {
$letter = strtolower($input[$x]);
if(!isset($statements[$letter])) {
continue;
}
// Do something if 'a', 'b', 'c'
// if($letter == 'a') etc ..
$output .= "" . $statements[$letter];
}
echo $output;
Mohammad's comment is probably the most concise answer for this problem, all credit goes to him.
Using str_replace
function makes it a one liner:
$str = 'cbaa';
$res = str_replace(['a', 'b', 'c'], ['65', '66', '67'], $str);
echo $res;
// prints 67666565 as expected