str_replace, gives me the wrong result Only when numerals are in it [duplicate]










-2
















This question already has an answer here:



  • str_replace with array

    4 answers



//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"










share|improve this question













marked as duplicate by Tim Biegeleisen, Phil php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 4:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 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 4:13






  • 2





    1 becomes 4A then the 4 is replaced with 2D so you get the 2DA.

    – user3783243
    Nov 14 '18 at 4:21






  • 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 4:24







  • 2





    Also, never roll your own JSON. Use echo json_encode(['username' => $print]); instead

    – Phil
    Nov 14 '18 at 4:26






  • 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 4:32
















-2
















This question already has an answer here:



  • str_replace with array

    4 answers



//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"










share|improve this question













marked as duplicate by Tim Biegeleisen, Phil php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 4:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 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 4:13






  • 2





    1 becomes 4A then the 4 is replaced with 2D so you get the 2DA.

    – user3783243
    Nov 14 '18 at 4:21






  • 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 4:24







  • 2





    Also, never roll your own JSON. Use echo json_encode(['username' => $print]); instead

    – Phil
    Nov 14 '18 at 4:26






  • 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 4:32














-2












-2








-2









This question already has an answer here:



  • str_replace with array

    4 answers



//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"










share|improve this question















This question already has an answer here:



  • str_replace with array

    4 answers



//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"





This question already has an answer here:



  • str_replace with array

    4 answers







php str-replace






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 4:11









Razu RazuRazu Razu

12




12




marked as duplicate by Tim Biegeleisen, Phil php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 4:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Tim Biegeleisen, Phil php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 4:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 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 4:13






  • 2





    1 becomes 4A then the 4 is replaced with 2D so you get the 2DA.

    – user3783243
    Nov 14 '18 at 4:21






  • 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 4:24







  • 2





    Also, never roll your own JSON. Use echo json_encode(['username' => $print]); instead

    – Phil
    Nov 14 '18 at 4:26






  • 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 4:32













  • 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 4:13






  • 2





    1 becomes 4A then the 4 is replaced with 2D so you get the 2DA.

    – user3783243
    Nov 14 '18 at 4:21






  • 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 4:24







  • 2





    Also, never roll your own JSON. Use echo json_encode(['username' => $print]); instead

    – Phil
    Nov 14 '18 at 4:26






  • 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 4:32








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 4:13





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 4:13




2




2





1 becomes 4A then the 4 is replaced with 2D so you get the 2DA.

– user3783243
Nov 14 '18 at 4:21





1 becomes 4A then the 4 is replaced with 2D so you get the 2DA.

– user3783243
Nov 14 '18 at 4:21




1




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 4:24






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 4:24





2




2





Also, never roll your own JSON. Use echo json_encode(['username' => $print]); instead

– Phil
Nov 14 '18 at 4:26





Also, never roll your own JSON. Use echo json_encode(['username' => $print]); instead

– Phil
Nov 14 '18 at 4:26




1




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 4:32






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 4:32













2 Answers
2






active

oldest

votes


















1














str_replace starts every replacement scan from the beginning and will replace parts of your prior replacements. Instead use strtr which won't.




The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.




E.g.:



echo strtr("12345", [
1 => '4A',
2 => '6B',
3 => '7C',
4 => '2D',
5 => '6F'
]);


will give you:



4A6B7C2D6F





share|improve this answer
































    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.






    share|improve this answer

























    • This is a nice explanation of the problem but doesn't exactly provide a solution.

      – Phil
      Nov 14 '18 at 4:28











    • Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.

      – Linus Juhlin
      Nov 14 '18 at 4:30











    • That is not my answer

      – Phil
      Nov 14 '18 at 4:31











    • Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.

      – Linus Juhlin
      Nov 14 '18 at 4:32

















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    str_replace starts every replacement scan from the beginning and will replace parts of your prior replacements. Instead use strtr which won't.




    The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.




    E.g.:



    echo strtr("12345", [
    1 => '4A',
    2 => '6B',
    3 => '7C',
    4 => '2D',
    5 => '6F'
    ]);


    will give you:



    4A6B7C2D6F





    share|improve this answer





























      1














      str_replace starts every replacement scan from the beginning and will replace parts of your prior replacements. Instead use strtr which won't.




      The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.




      E.g.:



      echo strtr("12345", [
      1 => '4A',
      2 => '6B',
      3 => '7C',
      4 => '2D',
      5 => '6F'
      ]);


      will give you:



      4A6B7C2D6F





      share|improve this answer



























        1












        1








        1







        str_replace starts every replacement scan from the beginning and will replace parts of your prior replacements. Instead use strtr which won't.




        The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.




        E.g.:



        echo strtr("12345", [
        1 => '4A',
        2 => '6B',
        3 => '7C',
        4 => '2D',
        5 => '6F'
        ]);


        will give you:



        4A6B7C2D6F





        share|improve this answer















        str_replace starts every replacement scan from the beginning and will replace parts of your prior replacements. Instead use strtr which won't.




        The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.




        E.g.:



        echo strtr("12345", [
        1 => '4A',
        2 => '6B',
        3 => '7C',
        4 => '2D',
        5 => '6F'
        ]);


        will give you:



        4A6B7C2D6F






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 4:30

























        answered Nov 14 '18 at 4:24









        PaulPaul

        17.4k103857




        17.4k103857























            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.






            share|improve this answer

























            • This is a nice explanation of the problem but doesn't exactly provide a solution.

              – Phil
              Nov 14 '18 at 4:28











            • Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.

              – Linus Juhlin
              Nov 14 '18 at 4:30











            • That is not my answer

              – Phil
              Nov 14 '18 at 4:31











            • Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.

              – Linus Juhlin
              Nov 14 '18 at 4:32















            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.






            share|improve this answer

























            • This is a nice explanation of the problem but doesn't exactly provide a solution.

              – Phil
              Nov 14 '18 at 4:28











            • Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.

              – Linus Juhlin
              Nov 14 '18 at 4:30











            • That is not my answer

              – Phil
              Nov 14 '18 at 4:31











            • Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.

              – Linus Juhlin
              Nov 14 '18 at 4:32













            0












            0








            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 14 '18 at 4:31

























            answered Nov 14 '18 at 4:26









            Linus JuhlinLinus Juhlin

            299413




            299413












            • This is a nice explanation of the problem but doesn't exactly provide a solution.

              – Phil
              Nov 14 '18 at 4:28











            • Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.

              – Linus Juhlin
              Nov 14 '18 at 4:30











            • That is not my answer

              – Phil
              Nov 14 '18 at 4:31











            • Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.

              – Linus Juhlin
              Nov 14 '18 at 4:32

















            • This is a nice explanation of the problem but doesn't exactly provide a solution.

              – Phil
              Nov 14 '18 at 4:28











            • Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.

              – Linus Juhlin
              Nov 14 '18 at 4:30











            • That is not my answer

              – Phil
              Nov 14 '18 at 4:31











            • Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.

              – Linus Juhlin
              Nov 14 '18 at 4:32
















            This is a nice explanation of the problem but doesn't exactly provide a solution.

            – Phil
            Nov 14 '18 at 4:28





            This is a nice explanation of the problem but doesn't exactly provide a solution.

            – Phil
            Nov 14 '18 at 4:28













            Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.

            – Linus Juhlin
            Nov 14 '18 at 4:30





            Yeah, I didn't find a good way to solve it. I thought about using strtr like you did in your answer, but couldn't recall that an array could be used for replacements.

            – Linus Juhlin
            Nov 14 '18 at 4:30













            That is not my answer

            – Phil
            Nov 14 '18 at 4:31





            That is not my answer

            – Phil
            Nov 14 '18 at 4:31













            Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.

            – Linus Juhlin
            Nov 14 '18 at 4:32





            Woops! Seems like I misread the name. My bad. I've updated my answer pointing to Paul's answer for the solution.

            – Linus Juhlin
            Nov 14 '18 at 4:32



            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto