#native_company# #native_desc#
#native_cta#

Use content from another site

By Dave Gross
on January 8, 2002

Version: 1.2

Type: Function

Category: HTML

License: Other

Description: This short script lets you take content from another page and embed it into your own pages. Just put the URL of the page who’s contents you would like and then in the start and finish lines, find something in the original source code that you can use to mark the start and finish cut points.

<?php

  function addBase( $baseUrl, $code )
  {
        // This is a very naive implementation, it doesn't properly
        // parse the "img" tag, but only looks for the string
        // '<img src="' - if the 'src' element comes somewhere else
        // within the tag, or if the filename it points to isn't
        // in double-quotes, this routine won't catch it.
        // Also this routine is chock full of magic numbers.  Whooop.
    $pCode = $code; $index = 0;
    while( $index < strlen( $code ) )
    {
      $srcPos = strpos( $pCode, "<img src="", $index );
      if( $srcPos != false )
      {
        // check for a ":" in the first 7 characters after 'src=', in which
        // case it's already a URL
        $colonStr = strpos( $pCode, ":", $srcPos );
        $subStrLen = $srcPos + 10 - $index; // 10 = size of "<img src="" string.
        echo( substr( $pCode, $index, $subStrLen) );
        $index += $subStrLen;
        if( ($colonStr < 12) || ($colonStr > 16) || ($colonStr === false) )
            // somewhat arbitrary
        {
          // modify the string by adding the $baseUrl
          echo( $baseUrl );
        }
      }
      else
      {
        $subStrLen = strlen( $code ) - $index;
        echo( substr( $pCode, $index, $subStrLen) );
        $index = strlen( $code );
      }
    }
  }

  function report( $message, $report )
  {
    if( $report != FALSE )
      echo( $message );
  }

  function includeFrom( $fromUrl, $baseUrl, $startString, $endString, $report )
  // $fromUrl - URL to read from, e.g. "http://foo.bar.com/index.html"
  // $baseUrl - Base to apply to local resources, e.g. "http://foo.bar.com/"
  // $startString - String before content you want to include
  // $endString - String after content you want to include
  // $report - "FALSE" if you don't want includeFrom() to insert error
  //           reporting into the output; errors will just cause no output
  {
    $fd = fopen( $fromUrl, "r" );
    if( $fd != FALSE )
    {
      $fr = fread( fopen( $fromUrl, "r" ), 100000 );
      fclose( $fd );
        
      if ($fr) {
        $start = strpos( $fr, $startString ) + strlen($startString);
        $finish = strpos( $fr, $endString ); // - strlen($endString);
        if( ($start != FALSE) && ($finish != FALSE) )
        {
          $length = $finish - $start;
          $code = Substr( $fr, $start, $length );
// if you don't want to get fancy, use echo( $code ) instead of addBase()
          addBase( $baseUrl, $code );
        }
        else
          report( "<! delimiter not found in $fromUrl >", $report );
      }
      else
        report( "<! could not read data from $fromUrl >", $report );
    }
    else
      report( "<! could not open $fromUrl >", $report );
  }

// Below here is just test code...
?>
<html>
 <body>
<?php
  includeFrom( "http://www.sfgate.com/weather/",
               "http://www.sfgate.com",
               "<!-- begin 5day forecast -->",
               "<!-- end 5day forecast -->",
               TRUE );
?>
 </body>
</html>