How to pick out a single digit in a number and replace it

Discussion in 'PHP' started by Ipodman79, Feb 6, 2014.

  1. #1
    So, I want to be able to get a digit, say all the number 5s from 423412534152345124 and change them to 7s or any other number. How do I do this?

    I know I can add 2 to the 5s but I don't know how to select just the 5s.

    Thanks
     
    Ipodman79, Feb 6, 2014 IP
  2. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #2
    something like this:
    <?php
    $a = "423412534152345124";
    $b = strtr($a, array("5"=>"7"));
    echo "$b";
    ?>
    Code (markup):
     
    malky66, Feb 6, 2014 IP
    ryan_uk likes this.
  3. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #3
    This sort of works. However I get '0.' and then 'E+64'. I just want this to change the numbers, so 5678 to something like, 5794.

    Thanks
     
    Ipodman79, Feb 6, 2014 IP
  4. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #4
    Weird, works fine for me, it outputs:
    423412734172347124
    Code (markup):
    when I run it.
     
    malky66, Feb 6, 2014 IP
  5. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #5
    But say the number 1000111010100000111101010000111001110001010111110000101011110001. It doesn't work.
     
    Ipodman79, Feb 6, 2014 IP
  6. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #6
    If i run this:
    <?php
    $a = "1000111010100000111101010000111001110001010111110000101011110001";
    $b = strtr($a, array("1"=>"7"));
    echo "$b";
    ?>
    Code (markup):
    it outputs:
    7000777070700000777707070000777007770007070777770000707077770007 
    Code (markup):
    so works fine for me
     
    malky66, Feb 6, 2014 IP
  7. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #7
    try replacing the 1s with 0s and the 0s with 1s.
     
    Ipodman79, Feb 6, 2014 IP
  8. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #8
    Ok, if i do this:
    <?php
    $a = "1000111010100000111101010000111001110001010111110000101011110001";
    $b = strtr($a, array("1"=>"0","0"=>"1"));
    echo "$b";
    ?>
    Code (markup):
    It outputs:
    0111000101011111000010101111000110001110101000001111010100001110 
    Code (markup):
    so like I said, it does work.
     
    malky66, Feb 6, 2014 IP
  9. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #9
    ryan_uk, Feb 7, 2014 IP
  10. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #10
    O
    i don't know, how do I check?
     
    Ipodman79, Feb 7, 2014 IP
  11. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #11
    ryan_uk, Feb 7, 2014 IP
  12. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #12
    I used that

    I used Konrad's solution and it looks like I'm using 64-bit php. However I need this to be able to run on 64 bit and 32 bit.

    Does that help?
     
    Ipodman79, Feb 7, 2014 IP
  13. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #13
    You still haven't answered @ryan_uk's question. Do you treat your numbers as strings?

    As you can see, @malky66 uses quotes around his variables.
     
    bogi, Feb 7, 2014 IP
  14. Ipodman79

    Ipodman79 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #14
    I have

    
    $number = 11010100101001101010101;
    
    PHP:
    Is that treating it as a string?
     
    Ipodman79, Feb 7, 2014 IP
  15. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #15
    Try to use the var_dump() function:

    var_dump( $number );
    PHP:
    You'll see, it's an integer ;) Now you can use single quotes and check again; it'll be a string.
     
    bogi, Feb 7, 2014 IP
  16. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #16
    No, you can convert in different ways, e.g.:

    $string = "$number";
    $string = (string) $number;
    $string = strval($number);
    
    PHP:
    Research it as there are pros and cons to some methods and also additional ones such as sprintf, settype, etc.

    Or even handle it as a string from the start.
     
    ryan_uk, Feb 7, 2014 IP
    malky66 likes this.