samoi Posted November 17, 2009 Share Posted November 17, 2009 Hello guys! I just wanted to do something fun like a special code decoding !! so I coded the following: <? error_reporting(E_ALL & E_NOTICE); $str = "samoi"; $char = " "; $i = 0; while($i < strlen($str)){ $subtype = substr($str, $i, 1); switch ($subtype) { case "s": $char. = "S"; break; case "m": $char. = "U"; break; case "a": $char. = "L"; break; case "o"; $char. = "I"; break; case "i"; $char. = "M"; break; default: $char. = $subtype; } $i++; } echo $char; ?> returns a blank page !!! please help! Quote Link to comment https://forums.phpfreaks.com/topic/181807-while-switch-problem/ Share on other sites More sharing options...
smerny Posted November 17, 2009 Share Posted November 17, 2009 there's a better way to do this using arrays and preg_replace... try this $find = array('s','m','a','o','i'); $replace = array('S','U','L','I','M'); echo preg_replace ($find, $replace, $str); Quote Link to comment https://forums.phpfreaks.com/topic/181807-while-switch-problem/#findComment-958841 Share on other sites More sharing options...
mikesta707 Posted November 17, 2009 Share Posted November 17, 2009 if you are just replacing single characters, and not matching against patterns, using str_replace would be faster Quote Link to comment https://forums.phpfreaks.com/topic/181807-while-switch-problem/#findComment-958842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.