Jump to content

does any know whats wrong here, str_replace


optikalefx

Recommended Posts

<?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

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);
?>

 

 

 

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.

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.

 

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.