2

How can i replace unused section in content. For example:

$content = "
    Test content

    [section]
        Section Content
    [section]

    End.
";

$content = preg_replace('/\[section\](.*)\[section\]/', '', $content);

Thanks

1
  • add the s modifier to match newlines with .. Also don't forget to make your expression ungreedy /\[section\](.*?)\[section\]/s Commented Nov 23, 2013 at 22:10

2 Answers 2

3

The s modifier allows a dot metacharacter in the pattern to match all characters, including newlines.

The i modifier is used for case-insensitive matching allowing both upper and lower case letters. By adding the quantifier ? it makes for a non greedy match and matches the least amount possible.

$content = preg_replace('/\[section\].*?\[section\]/si', '', $content);

See Demo

Sign up to request clarification or add additional context in comments.

1 Comment

@adoweb If this worked, its nice to accept an answer and close the question.
2

What's the regex for? This should work and will run a lot quicker:

$content = explode('[section]',$content);
$content = $content[0].$content[2];

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.