replace last occurance of string in multi line data

A place to try and solve your RegEx problems.
Post Reply
User avatar
shalini

replace last occurance of string in multi line data

Post by shalini »

I have multiline data, in that I want to replace string at last occurrence in PHP. can any one provide suggestion for this
User avatar
Guest

Re: replace last occurance of string in multi line data

Post by Guest »

I have data as below

"foo
foo
foo
foo
foo
bar
foo
test data
test data"


I want the regular expression or the way how to replace data after last "foo" variable.

Any quick response is helpful.

Thanks.
User avatar
Fool4UAnyway

Re: replace last occurance of string in multi line data

Post by Fool4UAnyway »

Basically, you want to search for:
- any particular string and anything following it, any number of time
- the same particular string
- some text after that

And replace that by
- "any particular string and anything following it, any number of time"
- "the same particular string"
- new text after all above

You have to capture the first two parts of the regular expression and then replace the "text after" with the "new text after".
Post Reply