Jump to content

String Replace and Arrays not working properly


patriotfan

Recommended Posts

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?

 

 

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.