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); Link to comment https://forums.phpfreaks.com/topic/151199-got-a-problem-with-str_replace/ 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)); Link to comment https://forums.phpfreaks.com/topic/151199-got-a-problem-with-str_replace/#findComment-794266 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. Link to comment https://forums.phpfreaks.com/topic/151199-got-a-problem-with-str_replace/#findComment-794275 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 Link to comment https://forums.phpfreaks.com/topic/151199-got-a-problem-with-str_replace/#findComment-794278 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 Link to comment https://forums.phpfreaks.com/topic/151199-got-a-problem-with-str_replace/#findComment-794284 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 ?> Link to comment https://forums.phpfreaks.com/topic/151199-got-a-problem-with-str_replace/#findComment-794319 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... Link to comment https://forums.phpfreaks.com/topic/151199-got-a-problem-with-str_replace/#findComment-794371 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.