Link to home
Start Free TrialLog in
Avatar of Geoff Millikan
Geoff MillikanFlag for United States of America

asked on

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:
<?php
$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.

Help!
Avatar of hernst42
hernst42
Flag of Germany image

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.
Avatar of Geoff Millikan

ASKER

...there is no way to avoid the 10GB memory usage due the internal structure...

Dang.  You sure?  Not possible with http://us3.php.net/manual/en/language.references.whatdo.php

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: https://bugs.php.net/bug.php?id=40479&edit=1

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?

Thanks,

t1shopper

PS.  The function truncate() would work great here but it removes data from the trailing end of the file, not the front.
SOLUTION
Avatar of Derokorian
Derokorian
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial