x-Boy Posted March 26, 2009 Share Posted March 26, 2009 I got a problem with str_replace, if you try the code below you'll see it doesn't return the assigned pronunciation correctly, and I don't know what's going wrong. I wonder if somebody can tell me what's going wrong and how I can solve it? function abc_pronunciation($incoming_input) { $arr_search = 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'); $arr_replace = array(' ay ',' be ',' ce ',' dee ',' ee ',' aef ',' gee ',' aich ',' ii ',' jay ',' kay ',' el ',' em ',' en ',' oh ',' pee ',' qu ',' ar ',' as ',' tee ',' yu ',' vee ',' doble-yu ',' ex ',' ye ',' zee '); $output = str_replace($arr_search,$arr_replace,$incoming_input); echo $output; } $input = "barcelona"; abc_pronunciation($input); Quote Link to comment Share on other sites More sharing options...
FaT3oYCG Posted March 26, 2009 Share Posted March 26, 2009 scrap that this one works i just tried it myself function abc_pro($incoming_input) { $arr_search = 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'); $arr_replace = array(' ay ',' be ',' ce ',' dee ',' ee ',' aef ',' gee ',' aich ',' ii ',' jay ',' kay ',' el ',' em ',' en ',' oh ',' pee ',' qu ',' ar ',' as ',' tee ',' yu ',' vee ',' doble-yu ',' ex ',' ye ',' zee '); $output = $incoming_input; for($i=1;$i<=26;$i++) { $output = str_replace($arr_search[$i], $arr_replace[$i], $output); } echo $output; } $input = "barcelona"; echo(abc_pro($input)); Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted March 26, 2009 Share Posted March 26, 2009 You might want to look into strtr instead of string replace. Quote Link to comment Share on other sites More sharing options...
x-Boy Posted March 26, 2009 Author Share Posted March 26, 2009 Thank you VERY MUCH..... Can you explain : for($i=1;$i<=26;$i++) { $output = str_replace($arr_search[$i], $arr_replace[$i], $output); } Im a beginner Quote Link to comment Share on other sites More sharing options...
x-Boy Posted March 26, 2009 Author Share Posted March 26, 2009 ok understood the loop.... thanks!!! This topic is solved, but why it didn't worked my way? I mean why you got to use a loop? (asking to learn more... hehe) thanks again FaT3oYCG and genericnumber1 Quote Link to comment Share on other sites More sharing options...
thebadbad Posted March 26, 2009 Share Posted March 26, 2009 Both functions don't work (for me) and shouldn't work, because when b is replaced with be, the e in be is then replaced with ce and so on. As genericnumber1 said, strtr() should be used: <?php function abc_pro ($str) { $replace_pairs = array( 'a' => ' ay ', 'b' => ' be ', 'c' => ' ce ', 'd' => ' dee ', 'e' => ' ee ', 'f' => ' aef ', 'g' => ' gee ', 'h' => ' aich ', 'i' => ' ii ', 'j' => ' jay ', 'k' => ' kay ', 'l' => ' el ', 'm' => ' em ', 'n' => ' en ', 'o' => ' oh ', 'p' => ' pee ', 'q' => ' qu ', 'r' => ' ar ', 's' => ' as ', 't' => ' tee ', 'u' => ' yu ', 'v' => ' vee ', 'w' => ' double-yu ', 'x' => ' ex ', 'y' => ' ye ', 'z' => ' zee ' ); return strtr($str, $replace_pairs); } echo abc_pro('barcelona'); //output: be ay ar ce ee el oh en ay ?> Quote Link to comment Share on other sites More sharing options...
x-Boy Posted March 26, 2009 Author Share Posted March 26, 2009 thats the methos I used. I have noticed this error in FaT3oYCG's function, but thanks for the advice any way... Quote Link to comment 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.