If the file is really large, you could run out of memory.
Doing a preg_replace or other string operation temporarily gives you two strings, the original and the new one. If the string is really large, having two might be enough to exceed memory limits.
The simplest solution is to increase the memory limit. This will take care of the PHP aspect:
ini_set('memory_limit', '64M');
Your OS may have per-process memory limits imposed too, which won't be affected by that setting. But most likely it is the PHP limit that you're hitting.
The best way to handle the situation is to process the files while you read them, instead of reading the whole file into memory all at once. That can be more difficult to code, though.