?
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
Medium Priority
?
259 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
[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
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Sander Stad
ID: 22700778
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
ID: 22700817
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 2000 total points
ID: 22700883
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
ID: 22701089
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
ID: 22701729
I've worked out the answer.

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

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

771 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