ckrudelux Posted June 24, 2009 Share Posted June 24, 2009 Okay so have I have some stuff that needs to be replaced and the strreplace works just great butI don't like to wirte the arrays on two seperate rows I would rather have them at the same row.EXAMPLE: Like I don't like to do it. $input = "this is a text with abc";$replace = array("a","b","c");$to = array("1","2","3");$output = strreplace($replace, $to, $input); This is annoying to keep track on which belongs to which.would be easier if the array look something like this: $replace = array("a=1","b=2","c=3"); Hope someone understands what I mean and can tell if I can do an array like that and how I make it work with strreplace. Link to comment Share on other sites More sharing options...
yoursanjay Posted June 24, 2009 Share Posted June 24, 2009 You can do it as follows:SYNOPSIS strReplace -find [TEXT TO FIND] -replace [TEXT TO SUBSITUTE IN] -string [sTRING TO BE SEARCHED]EXAMPLES strReplace -find pizza -replace hot dogs -string I like pizza. (returns: I like hot dogs.) strReplace -find a -replace o -string Banana Camera (returns: Bonono Camero) Link to comment Share on other sites More sharing options...
Synook Posted June 24, 2009 Share Posted June 24, 2009 You mean something like: $replace = array("a" => "1","b" => "2","c" => "3");$output = $input;foreach ($replace as $original => $to) { $output = str_replace($original, $to, $output);} Link to comment Share on other sites More sharing options...
ckrudelux Posted June 24, 2009 Author Share Posted June 24, 2009 You mean something like:$replace = array("a" => "1","b" => "2","c" => "3");$output = $input;foreach ($replace as $original => $to) { $output = str_replace($original, $to, $output);} Yes whats that I wanted. :)Okay so just so I understand this. "foreach()" is used to exicute on every line you get from the "$replace" as means the line and $original is the name for the first and "=>" means what they are equal but why the arrow? and $to is the second value. What are the rules in doing this kind of array? foreach ($replace as $original => $to) Link to comment Share on other sites More sharing options...
ckrudelux Posted June 24, 2009 Author Share Posted June 24, 2009 Then I'm on the topic, is this case sensitive, does it under stand what [bLOCK] and [block] is the same? Link to comment Share on other sites More sharing options...
boen_robot Posted June 24, 2009 Share Posted June 24, 2009 "foreach" takes an array and loops over it. The first variable (before the "=>") is a temporary variable, with which you label the currently looped over key. The second variable (after the "=>") is a temporary variable, with which you label the currently looped over value. So, with foreach ($replace as $original => $to) { in the first loop, $original contains "a", and $to contains "1". At the second loop, $original contains "b", and $to contains "2", and so forth. The loop automatically stops when there are no more array members (in this case, after "c").foreach also has a form in which you address only the value. For example: foreach ($replace as $to) { Is the same as above, but you won't get the array keys within the loop.Note that using for each instead of a single array may be simpler for the eye, but it's less efficient. str_replace supports using arrays as values, and thus, if you pass an array, PHP only has to call the str_replace function once. When you use foreach, the str_replace function is called the number of times you need to check for things.To combine easier readability and efficiency, consider $replace = array("a","b","c");$to = array("1","2","3"); Or at worst $replace = array(0=>"a",1=>"b",2=>"c");$to = array(0=>"1",1=>"2",2=>"3"); Either way, it's easier to see which belongs to which. Especially in the second case, where you can just look at the keys, and know that each $replace key corresponds to the same $to key. Link to comment Share on other sites More sharing options...
ckrudelux Posted June 24, 2009 Author Share Posted June 24, 2009 "foreach" takes an array and loops over it. The first variable (before the "=>") is a temporary variable, with which you label the currently looped over key. The second variable (after the "=>") is a temporary variable, with which you label the currently looped over value. So, withforeach ($replace as $original => $to) { in the first loop, $original contains "a", and $to contains "1". At the second loop, $original contains "b", and $to contains "2", and so forth. The loop automatically stops when there are no more array members (in this case, after "c").foreach also has a form in which you address only the value. For example: foreach ($replace as $to) { Is the same as above, but you won't get the array keys within the loop.Note that using for each instead of a single array may be simpler for the eye, but it's less efficient. str_replace supports using arrays as values, and thus, if you pass an array, PHP only has to call the str_replace function once. When you use foreach, the str_replace function is called the number of times you need to check for things.To combine easier readability and efficiency, consider $replace = array("a","b","c");$to = array("1","2","3"); Or at worst $replace = array(0=>"a",1=>"b",2=>"c");$to = array(0=>"1",1=>"2",2=>"3"); Either way, it's easier to see which belongs to which. Especially in the second case, where you can just look at the keys, and know that each $replace key corresponds to the same $to key. This was really good to know thanks alot Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.