Jump to content

Grab chunk of HTML


Canman2005

Recommended Posts

Hi all

 

I have the following code

 

if ($source = file_get_contents("http://help.websiteos.com/websiteos/example_of_a_simple_html_page.htm")) {
  $search = "<h1>Example of a simple HTML page</h1>";
  $newText = substr($source,strpos($source, $search)+strlen($search), 100);
  echo $newText;
} else {
print "error";
}

 

This grabs the first 100 characters of the HTML code from an external source starting from the defined section of code.

 

Rather than grabbing the first 100 characters, is there a way to define an end tag? So it would grab everything from the start and end tags?

 

Does that make much sense?

 

Thanks very much

 

Dave

Link to comment
Share on other sites

preg_match

 

Regular Expressions is where you want to go.

 

<?php
if ($source = file_get_contents("http://help.websiteos.com/websiteos/example_of_a_simple_html_page.htm")) {
   $search = "~<h1>(.*)</h1>~s";
   preg_match($search, $source, $match);
   echo $match[1];
} else {
   echo "error";
}

?>

Link to comment
Share on other sites

Thank you very much

 

What would be the best way to grab the following from the external HTML

 

<table id="Table1" cellspacing="1" cellpadding="1" border="1">
  <tr>
    <td height="18" colspan="2"><a id="_ctl5_DesktopThreePanes1_ThreePanes__ctl6_LbShoppingCart" href="javascript:__doPostBack('_ctl5$DesktopThreePanes1$ThreePanes$_ctl6$LbShoppingCart','')" style="font-size:X-Small;font-weight:bold;"> View Cart</a> </td>
  </tr>
  <tr>
    <td height="18"><span id="_ctl5_DesktopThreePanes1_ThreePanes__ctl6_Label3" style="font-size:XX-Small;">Line Items</span></td>
    <td height="18"><span id="_ctl5_DesktopThreePanes1_ThreePanes__ctl6_LblTotalLineItems" style="font-size:XX-Small;">0</span></td>
  </tr>
  <tr> </tr>
  <tr>
    <td colspan="2"></td>
  </tr>
  <tr>
    <td><span id="_ctl5_DesktopThreePanes1_ThreePanes__ctl6_Label1" style="font-size:XX-Small;width:56px;">SubTotal</span></td>
    <td><span id="_ctl5_DesktopThreePanes1_ThreePanes__ctl6_LblSubTotal" style="font-size:XX-Small;">$Zero</span></td>
  </tr>
</table>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.