Nyla Posted November 21, 2014 Share Posted November 21, 2014 (edited) May I please ask one more question? About replacing a string only if it appears *after* a particular string? I'm having trouble replacing all occurrences of "wheel" with "TIRE" but only when the word "wheel" is after this: (W/ Example: From this: A large wheel is shiny. Big 4 wheel truck (W/ wheel) Car with a wheel, and a small toy (w/ wheel) Four wheelers are cool To this: A large wheel is shiny. Big 4 wheel truck (W/ TIRE) Car with a wheel, and a small toy (w/ TIRE) Four wheelers are cool Unfortunately, I thought this worked: $fixed = str_ireplace('wheel','tire',substr($original_string,stripos($original_string,'(W'))); ...until I realized it was *cutting off* all characters up until that first '(W/' What can be done to make this work? Edited November 21, 2014 by Nyla Quote Link to comment Share on other sites More sharing options...
requinix Posted November 21, 2014 Share Posted November 21, 2014 What about replacing "(W/ wheel)" with "(W/ TIRE)"? Quote Link to comment Share on other sites More sharing options...
Nyla Posted November 21, 2014 Author Share Posted November 21, 2014 Hi again! Thank you for helping me yesterday! Regarding replacing with "(w/tire)," no that won't work because sometimes it's different words after the "w/" (for example, w/ door, w/ hood, etc., and since it's a 100mb text file with 500,000 lines, I won't be able to write a specific str_replace for each one, especially since I don't know, ahead of time, what comes after the "w/" part). I know I'm not articulating the reason "why not" very well, but I know that once I have a line of code, I can then apply the "principle" to all the other str_ireplaces that I have to do. Oops something just occured to me -- UNLESS I loop "line-by-line" (like when you helped me yesterday), PHP will treat all 500,000 lines as ONE BIG STRING, and if we say "any occurence of 'wheel' as long as it's after the "W/," then that won't work because with str_ireplace, it will do the ENTIRE FILE after the very first "W/" thingy, right? Uh - oh So, I guess that leaves using preg_replace (and I don't want to do that because I am terrible at preg stuff), and looping line-by-line, right? (Unless I am TOTALLY goofy-wrong on this, or unless you're seeing something I am missing?) Anyway, I NOW KNOW how to loop line-by-line (because you taught me how yesterday *yeah!*), so I'll put the str_ireplace in my "loop line by line" code. I just don't know how to fix my code without making it cut off that first part. My brain feels really really small right now Quote Link to comment Share on other sites More sharing options...
requinix Posted November 21, 2014 Share Posted November 21, 2014 Regarding replacing with "(w/tire)," no that won't work because sometimes it's different words after the "w/" (for example, w/ door, w/ hood, etc., and since it's a 100mb text file with 500,000 lines, I won't be able to write a specific str_replace for each one, especially since I don't know, ahead of time, what comes after the "w/" part).Either you missed that I said "replace (w/ wheel)" and not to replace everything w/*, or you just contradicted yourself when you said earlier occurrences of "wheel" with "TIRE" but only when the word "wheel" is after this: (W/ Quote Link to comment Share on other sites More sharing options...
Nyla Posted November 21, 2014 Author Share Posted November 21, 2014 So, here's what I'm trying to accomplish: to loop line by line through all 500,000 lines. Any time the word "wheel" occurs AFTER the word "cat", I want it replaced with tire: Original: The cat jumped over the wheel. My four wheels are cool. My car has a wheel and a cat on another wheel. Desired result: The cat jumped over the tire. My four wheels are cool. My car has a wheel and a cat on another tire. Quote Link to comment Share on other sites More sharing options...
davidannis Posted November 21, 2014 Share Posted November 21, 2014 (edited) You need to check if cat is in your string, if wheel is after and if so replace if (strpos('cat',$line) && (strpos('wheel',$line)>strpos('cat',$line))){ //replace things here } Edited November 21, 2014 by davidannis Quote Link to comment Share on other sites More sharing options...
Nyla Posted November 21, 2014 Author Share Posted November 21, 2014 Dr. David Annis, thank you -- but the problem I'm having comes from PHP wanting to replace the "wheel" that comes before the "cat". (haha -- it's kinda funny, you gotta laugh.... we're talking about cats and strig, got a whole zoo goin' on in here LOL) So anyway, if my original string is $string = "The wheel is too big for the cat's wheel"; I only want to replace the "wheel" that comes AFTER the word "cat." $corrected = "The wheel is too big for the cat's tire"; Your code suggestion will first look to see if "wheel" comes after "cat" (yes, it does), and then the string-replace will replace all occurences of "wheel," which is not what I want (I only want to replace the instances of "wheel" that come after "cat." MY code -- from my first post: $fixed = str_ireplace('wheel','tire',substr($original_string,stripos($original_string,'(W'))); -- WILL *ALMOST* work... but what happens is that it "cuts off" the first part of my string. So, in a nutshell (laymans terms), how to we make PHP only replace a string that comes after another given string? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2014 Share Posted November 21, 2014 $lines = array( "The cat jumped over the wheel.", "My four wheels are cool.", "My car has a wheel and a cat on another wheel." ); foreach ($lines as &$line) { $p = strpos($line, 'cat'); if ($p===false) continue; $part1 = substr($line, 0, $p); $part2 = substr($line, $p); $part2 = str_replace('wheel', 'tire', $part2); $line = $part1.$part2; } echo '<pre>',print_r($lines, true),'</pre>'; /* RESULTS ********************************************** Array ( [0] => The cat jumped over the tire. [1] => My four wheels are cool. [2] => My car has a wheel and a cat on another tire. ) */ 1 Quote Link to comment 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.