Jump to content

Newbie String Replace Question


Nyla

Recommended Posts

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 by Nyla
Link to comment
Share on other sites

Hi again! Thank you for helping me yesterday! :happy-04:

 

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 :shrug:

 

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?) :geek:

 

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 :confused:

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites


$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.
)
*/
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.