1

I have been searching for a number of hours now for a solution to remove a block of text within a file using PHP script.

There appears to be a number of options for removing lines of text, but not a block of text. I have attempted to use preg_replace ("/^$start.*?$end/s", $replace, $fdata) with the following, but have not found a solution that works.

I am sure that someone has done this already, so any help would be much appreciated.

$start = "# Copyright 2000-";
$end = "Agreement.";

# This software product may only be used strictly in accordance
# with the applicable written License Agreement.
3
  • 6
    Is this software you're writing? Or software you're trying to steal? Commented Aug 6, 2012 at 15:43
  • 1
    Now matt let's give him the benefit of the doubt, he could be open sourcing his own software, or updating a licence for software he or his company wrote. Commented Aug 6, 2012 at 15:45
  • 1
    Not stealing the software, but was updating the source code for my companies product...we ship source code and it's a requirement to have the copyright statement. Thanks VoroniPatato Commented Feb 6, 2013 at 23:10

1 Answer 1

5

You need multi-line mode (/m), otherwise your regex won't capture across multiple lines. Also, you should escape your regex parameters with preg_quote(), otherwise you may get undesired results (for example, in $end, it has a dot, which is a regex metacharacter, when you want it to match a single period.)

$regex = "/^" . preg_quote( $start, '/') .".*?". preg_quote( $end, '/') . "/sm";
preg_replace ( $regex, $replace, $fdata);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.