Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

use PHP to remove content between two tags

Posted on 2012-09-13
5
Medium Priority
?
278 Views
Last Modified: 2012-12-19
I want to remove content using PHP between two tags -

<!--BEGIN FOOTER-->

and

<!--END FOOTER-->

Any help would be great!
0
Comment
Question by:hypervisor
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1500 total points
ID: 38397419
Perhaps:

$beginFooterIndex = strpos($text, "<!--BEGIN FOOTER-->");
$endFooterIndex = strpos($text, "<!--END FOOTER-->");

$result = substr($text, 0, $beginFooterIndex) . substr($text, $endFooterIndex - strlen($text));

Open in new window

0
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 38397429
There may be a more elegant solution but this will work as long as the delimiters only occur once in the file.

<?php

$string = "Stuff that is at teh beginning
<!--BEGIN FOOTER-->

Stuff that I don't want to see
<!--END FOOTER-->

More stuff at the end.....";

$start = explode('<!--BEGIN FOOTER-->', $string);
$end = explode('<!--END FOOTER-->', $string);

$out = $start[0].'<!--BEGIN FOOTER--><!--END FOOTER-->'.$end[1];
echo $out;

?>

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38397458
I think I screwed mine up (forgot that the two tags would be interpreted as HTML!). If you want to remove the tags as well, then swith my previous comment to:

$beginFooterIndex = strpos($text, "<!--BEGIN FOOTER-->");
$endFooterIndex = strpos($text, "<!--END FOOTER-->")  + strlen("<!--END FOOTER-->");

$result = substr($text, 0, $beginFooterIndex) . substr($text, $endFooterIndex - strlen($text));

Open in new window


If you instead want to keep the tags, then you could do:

$beginFooterIndex = strpos($text, "<!--BEGIN FOOTER-->") + strlen("<!--BEGIN FOOTER-->");
$endFooterIndex = strpos($text, "<!--END FOOTER-->");

$result = substr($text, 0, $beginFooterIndex) . substr($text, $endFooterIndex - strlen($text));

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38399272
PHP is used to create HTML documents.  If you don't want the content, don't create it!

But if you're scraping someone else's web site and want to remove something in the footer (like maybe a copyright?) you can do it by any number of different ways.  Here's my take on it.  Please don't take content that is not yours to take.
http://www.laprbass.com/RAY_temp_hypervisor.php

<?php // RAY_temp_hypervisor.php
error_reporting(E_ALL);

$htm = <<<HTM
I want to remove content using PHP between two tags -

<!--BEGIN FOOTER-->

and

<!--END FOOTER-->

Any help would be great!
HTM;

$arr = explode('<!--BEGIN FOOTER-->', $htm);
$brr = explode('<!--END FOOTER-->', $htm);
$new = $arr[0] . end($brr);
var_dump($new);

Open in new window

0
 
LVL 1

Expert Comment

by:urspsk
ID: 38575787
Hai,

Try this,

$content = <Your HTML Content>

$desiredValue = preg_replace('/\<!--BEGIN FOOTER-->.*\<!--END FOOTER-->/', '', $content);

Open in new window


Let me know any issues in this.

Thanks...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 count occurrences of each item in an array.
Suggested Courses

578 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