optikalefx Posted February 21, 2008 Share Posted February 21, 2008 <?PHP $text = "This is cheese"; $old = 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("z", "y", "x", "w", "v", "u", "t", "s", "r", "q", "p", "o", "n", "m", "l", "k", "j", "i", "h", "g", "f", "e", "d", "c", "b", "a"); echo str_replace($old,$new,$text); ?> im trying to replace each letter with the opposite letter Link to comment https://forums.phpfreaks.com/topic/92227-does-any-know-whats-wrong-here-str_replace/ Share on other sites More sharing options...
cooldude832 Posted February 21, 2008 Share Posted February 21, 2008 what is the output? Link to comment https://forums.phpfreaks.com/topic/92227-does-any-know-whats-wrong-here-str_replace/#findComment-472457 Share on other sites More sharing options...
KrisNz Posted February 21, 2008 Share Posted February 21, 2008 I don't think its possible to do it that way, though I can't fully get my head around why, hopefully someone else can explain. Here is another solution. <?php $text = "cheese is a kind of meat, a tasty yellow beef."; $keys = range('a','z'); $final = array_combine($keys,array_reverse($keys)); echo strtr($text,$final); ?> Link to comment https://forums.phpfreaks.com/topic/92227-does-any-know-whats-wrong-here-str_replace/#findComment-472496 Share on other sites More sharing options...
optikalefx Posted February 21, 2008 Author Share Posted February 21, 2008 Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /hermes/web04/b1609/pow.4ten/htdocs/secret.php on line 2 Fatal error: Call to undefined function  range() in /hermes/web04/b1609/pow.4ten/htdocs/secret.php on line 3 Sorry but i get errors. Link to comment https://forums.phpfreaks.com/topic/92227-does-any-know-whats-wrong-here-str_replace/#findComment-472799 Share on other sites More sharing options...
Daney11 Posted February 21, 2008 Share Posted February 21, 2008 I don't think its possible to do it that way, though I can't fully get my head around why, hopefully someone else can explain. Here is another solution. <?php $text = "cheese is a kind of meat, a tasty yellow beef."; $keys = range('a','z'); $final = array_combine($keys,array_reverse($keys)); echo strtr($text,$final); ?> works fine for me. Link to comment https://forums.phpfreaks.com/topic/92227-does-any-know-whats-wrong-here-str_replace/#findComment-472845 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.