Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

preg_replace help

Hello experts,

I'm struggling with preg_replace(), could you help me? I have a file that contains the following text, how do I use preg_replace to remove "[sidebar]" and "[/sidebar]", including all the text between the two tags? The text between the tags will vary, and I want to get rid of all of it.

[sidebar]
Grab this cool stuff for your site
Text is variable!
[/sidebar]

Open in new window



Thank you!
0
EffinGood
Asked:
EffinGood
1 Solution
 
Terry WoodsIT GuruCommented:
This should do it:
$text = preg_replace("#\[sidebar\].*?\[/sidebar\]#s","",$text);

Open in new window


The s pattern modifier at the end of the pattern string means the . wildcard will match \n characters as well as everything else, which allows the matched text to span multiple lines.
0
 
EffinGoodAuthor Commented:
Thank you Terry!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now