patriotfan Posted March 8, 2009 Share Posted March 8, 2009 I was working on a translator that uses 2 arrays and a str_replace But for some reason, 2 letters don't output as the should Ex. If I type in chris, I get cChrize_ instead of cChriz_ Here are the arrays: $orig = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); $new = array('aa', 'b', 'cC', 'de', 'E', 'f', 'gj', 'h', 'i', 'j', 'k', 'll', 'm', 'n', 'oo', 'pe', 'qu', 'r', 'z_', 'te', 'u', 've', 'wu', 'x', 'y', 'ze'); Whats wrong? Link to comment https://forums.phpfreaks.com/topic/148531-string-replace-and-arrays-not-working-properly/ Share on other sites More sharing options...
Maq Posted March 9, 2009 Share Posted March 9, 2009 Can you post more relevant code? Link to comment https://forums.phpfreaks.com/topic/148531-string-replace-and-arrays-not-working-properly/#findComment-780041 Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 I don't know how your translation code looks but this works $orig = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); $new = array('aa', 'b', 'cC', 'de', 'E', 'f', 'gj', 'h', 'i', 'j', 'k', 'll', 'm', 'n', 'oo', 'pe', 'qu', 'r', 'z_', 'te', 'u', 've', 'wu', 'x', 'y', 'ze'); $string = "chris"; $keys = array(); for($x = 0; $x < strlen($string); $x++) { $key = array_search(substr($string,$x,1), $orig); $keys[] = $key; } foreach($keys as $key) { $newString .= $new[$key]; } print $newString; Link to comment https://forums.phpfreaks.com/topic/148531-string-replace-and-arrays-not-working-properly/#findComment-780206 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.