Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts

    String Replace/Switch Question

    Hi, I want to switch two numbers, but when I try to do it within an array a little bit too much switching is happening and I'm not yielding the end result I'm looking for.

    Code:
    <?php
    
    $rawstring = "Replace 1 with 3, and 3 with 1.";
    
    $placeholders = array('1', '3');
    
    $replvals = array('3', '1');
    
    $newstr = str_replace($placeholders, $replvals, $rawstring);
    
    echo $newstr;
    
    ?>
    Thanks so much.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    PHP Code:
    <?php

    $rawstring 
    "Replace 1 with 3, and 3 with 1.";
    $newstr preg_replace('/\b(1|3)\b/''[$1]'$rawstring);
    $placeholders = array('[1]''[3]');
    $replvals = array('3''1');
    $newstr str_replace($placeholders$replvals$newstr);
    echo 
    $newstr;
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Amazing, thanks so much!

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    no problem
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    function str_swap$string, Array $replacements = array() )
    {
        if ( ! empty( 
    $replacements ) )
        {
            
    $search  array_keys$replacements );
            
    $replace array_values$replacements );
            foreach ( 
    $search as $i => $value )
            {
                
    $string str_replace$value'[[' $i ']]'$string );
            }
            foreach ( 
    $replace as $i => $value )
            {
                
    $string str_replace'[[' $i ']]'$value$string );
            }
        }
        return 
    $string;
    }

    // echoes: Replace 3 with 1, and 1 with 3.
    echo str_swap'Replace 1 with 3, and 3 with 1.', array( => 3=> ) ); 
    You can exchange the brackets for something more unique to ensure the changes are made properly. If [[0]], etc, already exists in the original string (rare but possible) it would throw the whole thing off.
    Last edited by kbluhm; 01-18-2010 at 05:27 PM.

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    I did consider the possibility of that, but the example looked so simple I didn't think it would matter
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Nothing like a good bit of overkill chaps.

    Code:
    $string = "Replace 1 with 3, and 3 with 1.";
    $string = str_replace(array('1', '3'), array('3', '1'), $string);
    
    print($string);

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    That produces
    Replace 1 with 1, and 1 with 1. which is why the examples above had the tags
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #9
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Oops. Duly chastised.

  • #10
    Regular Coder
    Join Date
    Jul 2007
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I like the logic of the function a lot. Thanks kbluhm.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •