Solved

How to I replace a block of text while specifying start and end tags and a replacement string?

Posted on 2008-10-12
5
250 Views
Last Modified: 2013-12-13
I need to replace a block of text e.g.

abcdefg
<start-tag>

this is some
text which
I want to remove

<end-tag
hijklmnop

With some some other text e.g.

new text

So the result should be...

abcdefg
new text
hijklmnop

I would expect the function to work like this

$result = str_replace_func($my_text, "<start-tag>", "<end-tag">, "new text");

0
Comment
Question by:mindwarpltd
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Sander Stad
Comment Utility
The str_replace function does not look between strings but searches for strings that are given. You could use the code I added.
What is does is it gets a variable with the text it has to search. Than a pattern is made, you could make this dynamic with variables in a function. Than the content what will replace the old.
At last the preg_replace function calls all teh three variables and replaces the text.

$p = '<style type="text/css"> css css css</style>'; 

$pattern = "/<style\b[^>]*>(.*?)<\/style>/ims"; 

$content = "test"; 

echo preg_replace($pattern, $content, $p); 

Open in new window

0
 

Author Comment

by:mindwarpltd
Comment Utility
I'm a little confused by the pattern. Can you show me what my pattern would look like?
0
 
LVL 9

Accepted Solution

by:
Sander Stad earned 500 total points
Comment Utility
It would look like this:
 

$p = '<start-tag>this is some text i want to remove</end-tag>'; 

$pattern = "/<sart-tag\b[^>]*>(.*?)<\/end-tag>/ims"; 

$content = "new text"; 

echo preg_replace($pattern, $content, $p); 

Open in new window

0
 

Author Comment

by:mindwarpltd
Comment Utility
Ooops...

I had to change the start and end tags...
I have...

$pattern = "/<!-- Start Foot Ad --\b[^>]*>(.*?)<!-- End Foot Ad -->/ims";


<!-- Start Foot Ad -->
whatever
<!-- End Foot Ad -->

Help
0
 

Author Comment

by:mindwarpltd
Comment Utility
I've worked out the answer.

$pattern = "/<!--StartFootAd-->(.*?)<!--EndFootAd-->/ims";
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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 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 …

743 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

19 Experts available now in Live!

Get 1:1 Help Now