Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-18-2010, 04:46 PM
PM User |
#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.
01-18-2010, 05:07 PM
PM User |
#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 ;
01-18-2010, 05:09 PM
PM User |
#3
Regular Coder
Join Date: Jul 2007
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
Amazing, thanks so much!
01-18-2010, 05:16 PM
PM User |
#4
Regular Coder
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
no problem
01-18-2010, 05:21 PM
PM User |
#5
Senior Coder
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
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( 1 => 3 , 3 => 1 ) );
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 ..
01-18-2010, 05:28 PM
PM User |
#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
01-18-2010, 05:48 PM
PM User |
#7
Senior Coder
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
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);
01-18-2010, 05:50 PM
PM User |
#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
01-18-2010, 06:03 PM
PM User |
#9
Senior Coder
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
Oops.
Duly chastised.
01-18-2010, 06:14 PM
PM User |
#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.
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 06:09 AM .
Advertisement
Log in to turn off these ads.