Implode, file_get_contents and substact string

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(''));

These days, I prefer to use file_get_contents instead of implode which does the same thing.

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('')); 

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

$end   = '</body>';

// 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.

See also substr.

web security linux ubuntu GIT svn Raspberry apache mysql php drupal cake symfony javascript Ajax css