Remove sections from string

Posted on 2005-05-01
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

    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

    Its Perfect! Thanks!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    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.

    728 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

    22 Experts available now in Live!

    Get 1:1 Help Now