Solved

preg_replace help

Posted on 2013-11-13
2
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 39646765
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
ID: 39646775
Thank you Terry!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 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