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
https://forums.phpfreaks.com/topic/154360-grab-chunk-of-html/
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
https://forums.phpfreaks.com/topic/154360-grab-chunk-of-html/#findComment-811516
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
https://forums.phpfreaks.com/topic/154360-grab-chunk-of-html/#findComment-811604
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.