• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

Remove sections from string

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.
Thanks,
Matt.
0
pyromatt
Asked:
pyromatt
1 Solution
 
Michael701Commented:
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

<?PHP
$x=strpos($file,'<tr>');
$answer=substr($file,0,$x); // everything up to first row of table
$file=substr($file,$x);
$x=strpos($file,'</tr>');
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.=$this_row;
  $x=strpos($file,'</tr>');
}
$answer.=$file; // remainder of the file

echo $answer;
?>
0
 
pyromattAuthor Commented:
Its Perfect! Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now