Logo

PHP implode and substact

Posted under » PHP on 06 October 2010

One of my backbone php commands of this CMS.

Let's get a web page into a string. It is often used with file php commands.

$html = implode('', file('http://www.example.com/'));

of course, you can use a local file as shown below instead of a remote file via http as shown above.

$html = implode('', file('/folder/thefile.html'));

There may cases where you want a certain portion of the page.

$mendel = implode('', file('http://mendel.com/index.html')); 

// substact the unwanted header -466 chars
$trimmed = substr($mendel, 466);

$end   = '';

// to find the unwanted footer. gives you 487 for eg.
$pos   = strripos($trimmed, $end);

// this time minus the header and footer
$trimmed = substr($mendel, 466, $pos);

// another way at looking
$trimmed = substr($mendel, 466, -487);


// header only
$trimmed = substr($mendel, 0, 100);


echo $trimmed;

See also String Replacer.