-2
//post data
$post = $_POST['username'];

//str replace
$print = str_replace([1,2,3,4,5], ['4A','6B','7C','2D','6F'], $post);

//prints result
echo "{\"username\":\"" . $print . "\"}";

str replace works fine when it's just alpha (example: TEST!@#test = 7160767124452671607671 which is just perfect, but as soon as digits come into play things get messy $print should output {"username":"4A6B7C2D6F"} if input 12345 the current output is {"username":"2DA6B7C2D6F"}

Razu Razu
  • 1
  • 2
  • 2
    Please show us clear input and the expected output with that input. I don't follow what you are trying to do here. – Tim Biegeleisen Nov 14 '18 at 04:13
  • expected output `{"username":"4A6B7C2D6F"}`if input `12345` the current output is `{"username":"2DA6B7C2D6F"}` – Razu Razu Nov 14 '18 at 04:15
  • 2
    `1` becomes `4A` then the `4` is replaced with `2D` so you get the `2DA`. – user3783243 Nov 14 '18 at 04:21
  • I see, I have had it in order, which worked fine if it's just numeral. The problem is I have alpha in my actual code, as well as symbols. That's why things are getting messy. – Razu Razu Nov 14 '18 at 04:23
  • 1
    Without your actual code, just about any answer to this may be wrong. Please provide real-world requirements and examples – Phil Nov 14 '18 at 04:24
  • 2
    Also, **never roll your own JSON**. Use `echo json_encode(['username' => $print]);` instead – Phil Nov 14 '18 at 04:26
  • I have gave real world examples and requirements, just because I haven't uploaded an entire thing, that has lots of stuff not needed for this question, I snipped it down to what you need to answer my question. – Razu Razu Nov 14 '18 at 04:30
  • 1
    So what exactly does _"I have alpha in my actual code, as well as symbols"_ mean? Are you or are you not **only** trying to replace digits 1 through 5 with 4A, 6B, 7C, 2D, 6F? – Phil Nov 14 '18 at 04:32
  • Yes I am, But it goes along with my alpha too, why do you think I uploaded what TEST!@#test = to, becauuse it's in there... is it required to answer this question? no it's not. – Razu Razu Nov 14 '18 at 04:34
  • You have not defined the relationship between `TEST!@#test` and `7160767124452671607671`. There are no digits in `TEST!@#test` so how can a replacement operation that replaces 1 through 5 affect that string at all? I only ask as one possible (but probably incorrect) answer would be to re-order the find / replace arrays so they don't conflict but this would only be possible if your were **only** replacing 1, 2, 3, 4 and 5 – Phil Nov 14 '18 at 04:39
  • `Please avoid extended discussions in comments. Would you like to automatically move this discussion to chat?` it's been answered, I won't be responding no more. Thankyou for your help! – Razu Razu Nov 14 '18 at 04:43
  • @RazuRazu look, you're already sitting on 2 downvotes. I'm just trying to help you not have that happen again. Take care – Phil Nov 14 '18 at 04:47

1 Answers1

0

Welcome to StackOverflow!

I found some information regarding your issue in the official PHP.net documentation.

Caution

Replacement order gotcha

Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.

In your case it's first replacing the 1 with 4A and then replacing the 4 in 4A with 2D resulting in 2DA and after that it seems to be replacing everything correctly.

Happy coding!

EDIT: Using strtr as illustrated in Paul's answer will give you the result you desire.

Linus Juhlin
  • 1,175
  • 10
  • 31