Solved

preg_replace help

Posted on 2013-11-13
2
307 Views
Last Modified: 2013-11-13
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
Comment
Question by:EffinGood
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:EffinGood
Comment Utility
Thank you Terry!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now