user3027056 user3027056 - 10 days ago 5
PHP Question

preg_replace() everything before a match to blank only once

Variable value:

1000+ lines of not required text

ABC: Required text.

ABC: More Required text.

Goal is to replace everything before the first "ABC:" i.e the 1000+ lines of not required text to blank. As far as I read, I need to use

preg_replace()
with the limit parameter to 1.

How do I go about doing this?

preg_replace("/.*/ABC:" , "ABC:", $string, 1);

Answer

You don't need preg_replace() when you can use strstr to do this.

echo strstr($string, 'ABC:');
Comments