amedhussaini Posted April 8, 2010 Share Posted April 8, 2010 I have a STRING full of text ($a) $search = array('one', 'two'); $replace = array('two', 'one'); $a = str_replace($search, $replace, $a); I want to replace 'one' with 'two' in a string if it exists, but i don't want it to revert back to 'two' as it traverses the next bit of the array. Does this make sense? cheers, Amed Quote Link to comment https://forums.phpfreaks.com/topic/197952-str_replace-how-to-skip-certain-searchreplace-rules/ Share on other sites More sharing options...
oni-kun Posted April 8, 2010 Share Posted April 8, 2010 preg_replace Quote Link to comment https://forums.phpfreaks.com/topic/197952-str_replace-how-to-skip-certain-searchreplace-rules/#findComment-1038732 Share on other sites More sharing options...
amedhussaini Posted April 8, 2010 Author Share Posted April 8, 2010 Interesting. Could you perhaps go into a bit more detail (using the example I did)? Quote Link to comment https://forums.phpfreaks.com/topic/197952-str_replace-how-to-skip-certain-searchreplace-rules/#findComment-1038735 Share on other sites More sharing options...
amedhussaini Posted April 8, 2010 Author Share Posted April 8, 2010 hrm.. this still seems to change my replacement right back as it traverses the array. i must be missing something fundamental: $search = array('/one/', '/1/'); $replace = array('1', 'one'); $a = preg_replace($search, $replace, $a); Just to reiterate, i want it to search through the $string and replace the 'one' it finds with '1' or change any '1' it finds to 'one' the FIRST time it searches through the string. so i'd like the string to transform from: i like one 1 one ////after replacement//// i like 1 one 1 Quote Link to comment https://forums.phpfreaks.com/topic/197952-str_replace-how-to-skip-certain-searchreplace-rules/#findComment-1038743 Share on other sites More sharing options...
teamatomic Posted April 8, 2010 Share Posted April 8, 2010 The only way I can think of doing what you want to do with a "double reverse" (for lack of a better term) in a string is by exploding the string into an array, making an array keyed to the changes then using array_walk to return a referenced array with the changes and imploding that array back to a string. Sounds like a lot but its only a small function and a few more lines of code. function swap($value,$key,&$s) { $k=array('1'=>'one','one'=>'1'); $s[]= $k[$value]; } $a='1 one 1 one'; $aa=explode(" ",$a); array_walk($aa,swap,&$s); $ss=implode(" ",$s); echo $ss; HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/197952-str_replace-how-to-skip-certain-searchreplace-rules/#findComment-1038821 Share on other sites More sharing options...
five Posted April 8, 2010 Share Posted April 8, 2010 i think this will work $string = "this is one 1 one string 1111one 11"; $replace = array('one'=>'1', '1'=>'one'); echo strtr($string, $replace); Quote Link to comment https://forums.phpfreaks.com/topic/197952-str_replace-how-to-skip-certain-searchreplace-rules/#findComment-1038886 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.