PHP CLI > Remove a leading block of text from string > Delete from string

So I have a huge 5GB string and I want to remove some leading text (from the start of the string).  How do I do that?  

For example:
$big_string     ='really_super_huge_string_but_do_not_remove_this_really_or_this_really';
$text_to_remove ='really_';

$big_string = str_replace($text_to_remove, "", $big_string, 1);

Open in new window

I thought the above would work but cannot use $big_string twice in str_replace() or we get, PHP Fatal error:  Only variables can be passed by reference

I don't want to make a copy of the $big_string variable because then I'm eating up 10GB of RAM.

Geoff MillikanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If the string is in memory, there is no way to avoid the 10GB memory usage due the internal structure how php handles variables.
If the content is in a file you can use the fget and fwrite function to strip that part of the string with low memory usage.
Geoff MillikanAuthor Commented:
...there is no way to avoid the 10GB memory usage due the internal structure...

Dang.  You sure?  Not possible with

Anyway, I rewrote it so that instead of erasing data in $big_string, I just crawl forwards and backwards over it using substr().  This works well for awhile but consistently crashes while accessing the same place in $big_string with the error "zend_mm_heap corrupted" which is not really fixable per open bug report at:

I'll try using fread() and fseek() to crawl the file on disk however past experience has shown fseek'ing past PHP_INT_MAX is really a pain.

Are you saying I can use fwrite() to remove data from the front of the file?



PS.  The function truncate() would work great here but it removes data from the trailing end of the file, not the front.
$length = strlen($text_to_remove);
$big_string = substr($big_string,$length);

I may not understand the problem as I've never had a single variable that's taking up that much resources, but it seems like this should work without copying the large variable.
No you read from one file and write to another file which excludes the part you don't want.

References do not work for str_replace or substr as in the C implementation of those function the string is copied.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hugh McCurdyCommented:
If the problem isn't solved yet, could you explain more about it?

Why do you have this big string?  Is this a one time edit of the string or something you'll do every day (with different big strings)?

Context is important.

(Aside: A customer asked me to quote a project.  I looked at the project and said, that will take 2 to 3 months.  I asked what he was trying to accomplish.  He told me.  I said that I could accomplish the same business effect by doing the project a different way and I'd be done in 6 hours.  Context is important.)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.