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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.