Remove sections from string

Posted on 2005-05-01
Medium Priority
Last Modified: 2012-05-07
I have a PHP script which loads a HTML document into a string (called $file). I also have a string named $filter. When the script is run, I would like it to go through $file and edit it appropriatly. Bacically, the html document contains a table with several rows. The script should go to the first <tr> tag and check for $filter between <tr> and the </tr> tag. If $filter is NOT found, then everything between the <tr> and the </tr> should be removed from the string. The script then goes to the next <tr> and does the same. It does this continually until no <tr> tags remain. It should then echo $file.
Question by:pyromatt
LVL 19

Accepted Solution

Michael701 earned 340 total points
ID: 13906744
Here's some untested code, you may have to tweak a value or so, but give it a try
Note: use of !== to check for type and value being false, don't change this

$answer=substr($file,0,$x); // everything up to first row of table
while ($x!==false) // is there still another row?
  $this_row=substr($file,0,$x+5); // get this row
  $file=substr($file,$x+5); // chop off this row
  if (strpos($this_row,$filter)!==false)
$answer.=$file; // remainder of the file

echo $answer;

Author Comment

ID: 13913472
Its Perfect! Thanks!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
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 dynamically set the form action using jQuery.
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

862 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