Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2010, 04:46 PM   PM User | #1
theflyingminstr
Regular Coder

 
Join Date: Jul 2007
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
theflyingminstr is an unknown quantity at this point
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.
theflyingminstr is offline   Reply With Quote
Old 01-18-2010, 05:07 PM   PM User | #2
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
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 :)
JAY6390 is offline   Reply With Quote
Old 01-18-2010, 05:09 PM   PM User | #3
theflyingminstr
Regular Coder

 
Join Date: Jul 2007
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
theflyingminstr is an unknown quantity at this point
Amazing, thanks so much!
theflyingminstr is offline   Reply With Quote
Old 01-18-2010, 05:16 PM   PM User | #4
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
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 :)
JAY6390 is offline   Reply With Quote
Old 01-18-2010, 05:21 PM   PM User | #5
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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.
__________________
ZCE

Last edited by kbluhm; 01-18-2010 at 05:27 PM..
kbluhm is offline   Reply With Quote
Old 01-18-2010, 05:28 PM   PM User | #6
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
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 :)
JAY6390 is offline   Reply With Quote
Old 01-18-2010, 05:48 PM   PM User | #7
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
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);
MattF is offline   Reply With Quote
Old 01-18-2010, 05:50 PM   PM User | #8
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
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 :)
JAY6390 is offline   Reply With Quote
Old 01-18-2010, 06:03 PM   PM User | #9
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Oops. Duly chastised.
MattF is offline   Reply With Quote
Old 01-18-2010, 06:14 PM   PM User | #10
theflyingminstr
Regular Coder

 
Join Date: Jul 2007
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
theflyingminstr is an unknown quantity at this point
I like the logic of the function a lot. Thanks kbluhm.
theflyingminstr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:09 AM.


Advertisement
Log in to turn off these ads.