1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Rep Power

    Extracting from html using php

    I am completely new to regexp, but i think i need some to extract some info from a table that i have, maybe somebody can give me some pointers?

    here is the html,

    <table width="430"  border="0" cellpadding="4">                     <tr>                       <td width="122" valign="top"><img src="graphics/ice_logo_1.gif"></td>                       <td width="130" valign="top"><p class="style3"><strong>Home Broadband</strong><br /><span class="tool">Wireless <a href="" title="">  <img src="graphics/tooltip.gif" width="10" height="10" border="0"></a></span><br /><em>Cont </em> (36:1) <a href=""  title=""><img src="graphics/tooltip.gif" width="10" height="10" border="0"></a><br>                         </p></td>                      <td width="100" valign="top"><span class="style3">                          <em>Dn</em>  					 3Mbps					  <em><a href="" title=""> <img src="graphics/tooltip.gif" width="10" height="10" border="0"></a></em><br>                           <em>Up</em>                            1Mbps</span>  <span><em><a href="" title=""><strong><img src="graphics/tooltip.gif" width="10" height="10" border="0"></strong></a></em></span></td>                       <td width="78" align="right" valign="top"><p> &euro;37.99 <a href="test" title=""> <strong><img src="graphics/tooltip.gif" width="10" height="10" border="0"></strong></a><br /> 					                        </p>                         </td>                     </tr>                   </table>
    can somebody help me get the fields from this?
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Shanghai, An tSín
    Rep Power
    Generally using your own regexps to parse HTML is a bad idea. I'm not PHP programmer, but I'd be pretty confident there should be HTML parsing libraries available to do this kind of task.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Pennsylvania, USA
    Rep Power
    This will give you all the contents of the <td> fields:
    PHP Code:
    $string = <<<STRING
    <table width="430"  border="0" cellpadding="4">                     <tr>                       <td width="122" valign="top"><img src="graphics/ice_logo_1.gif"></td>                       <td width="130" valign="top"><p class="style3"><strong>Home Broadband</strong><br /><span class="tool">Wireless <a href="" title="">  <img src="graphics/tooltip.gif" width="10" height="10" border="0"></a></span><br /><em>Cont </em> (36:1) <a href=""  title=""><img src="graphics/tooltip.gif" width="10" height="10" border="0"></a><br>                         </p></td>                      <td width="100" valign="top"><span class="style3">                          <em>Dn</em>                       3Mbps                      <em><a href="" title=""> <img src="graphics/tooltip.gif" width="10" height="10" border="0"></a></em><br>                           <em>Up</em>                            1Mbps</span>  <span><em><a href="" title=""><strong><img src="graphics/tooltip.gif" width="10" height="10" border="0"></strong></a></em></span></td>                       <td width="78" align="right" valign="top"><p> &euro;37.99 <a href="test" title=""> <strong><img src="graphics/tooltip.gif" width="10" height="10" border="0"></strong></a><br />                                             </p>                         </td>                     </tr>                   </table>

    However, it also includes plenty of HTML. If the HTML is properly formed, you can use the DOM document model to parse it, but I personally like using regexp for this sort of thing.

    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo