how to perg_match between <tr>and </tr> or <td>ot</td> in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mahesr
    New Member
    • Nov 2006
    • 7

    how to perg_match between <tr>and </tr> or <td>ot</td> in php

    I want to match some particular text between <tr>and </tr> or <td>and </td>....
    in PHP.

    like below.......... ..
    <table><tr>
    CATEGORY:
    <td><font face="Verdana" size="1" color="#A000A0" >
    Wedding

    Accessories
    </font></td>
    <td><font face="Verdana" size="1" color="#A000A0" >
    Wedding

    Crèche
    </font></td>
    <td>
    <font face="Verdana" size="1" color="#A000A0" >
    Wedding

    Gifts
    </font></td>


    <table>
    <tr>
    WORLDWIDE:

    FOR INTERNATIONAL SUPPLIERS CLICK HERE
    <td><font face="Verdana" size="2" color="#A000A0" ><b>
    Australia<b/>
    </font></td> <td><font face="Verdana" size="2" color="#A000A0" ><b>Granada Malta</b></font></td>


    BUT Want to match only category table(categorie s have same font style ,face ,size and color).

    and also want to match internation suppliers(pls look countries have <b></b>tags between <font></font> tags
  • TheMadMidget
    New Member
    • Oct 2006
    • 98

    #2
    I don't know of a direct way other than a string comparision.
    [PHP]
    $start = strpos($str, "string"); // use this to get right before the first tag
    $start = strpos($str, "string", $start); //(optional) use this to get right before the first tag if the line above only get to two tags before, use as many as neccessary
    $start = strpos($str, "<td>", $start) + 4;
    $end = strpos($str, "</td>", $start) ;
    $length = $end - $start;
    $contents = substr($str, $start, $length);
    [/PHP]

    It's a little crude, but works.

    Comment

    • steven
      New Member
      • Sep 2006
      • 143

      #3
      Sorry, do I understand that you want the text between the tags?

      The the expression should be something along the lines of...

      preg_match("/<td>(.*?)<\/td>/", $stringToSearch , $matches);

      This will capture all the text between the <td></td> tags. It will be in matches[1].

      You would need to read the html file into a string, using file_get_conten ts(). You could run a regular expression to capture just the table you want. But you should change your HTML, place the table titles in the <caption> tag, then you can modify the expression above to capture between caption tags.

      Comment

      • mahesr
        New Member
        • Nov 2006
        • 7

        #4
        hi all,


        thanks,but not work.i want to a text like

        Wedding
        Accessories

        Wedding
        Crèche

        only Between Category tr's or td's..pls look this have <font> tags...so easy to match using this font but do not know the code...

        Comment

        • TheMadMidget
          New Member
          • Oct 2006
          • 98

          #5
          after you get what in between the <td> / <tr> tag use
          $contents = strip_tags($con tents);
          This does exactly what it sounds like, it strips all tags so everything between a < and a > goes away, all of them. You can also chose to keep certain ones. For futher reference: http://us3.php.net/strip_tags.
          Cheers,
          Happy Thanksgiving

          Comment

          • ctsasikumar
            New Member
            • Nov 2006
            • 17

            #6
            Originally posted by TheMadMidget
            after you get what in between the <td> / <tr> tag use
            $contents = strip_tags($con tents);
            This does exactly what it sounds like, it strips all tags so everything between a < and a > goes away, all of them. You can also chose to keep certain ones. For futher reference: http://us3.php.net/strip_tags.
            Cheers,

            Happy Thanksgiving
            hai try this code..i think this is working perfectly

            preg_match_all( '/\<tr\>(.*?)\<\/tr>/is', $content, $matches);

            Comment

            Working...