PHP Quicky - Fetching File Content and Stripping HTML/PHP

2 replies
Hello fellow warriors,

Category: I/O & Filtering
Difficulty: Beginner

This quicky is useful for those of us who need to quickly strip out all HTML and PHP from a file. Say you have a webpage that you would like to only see the content without the tags, well.. this tip is for you:

the fgetss() function read from a file and outputs the content with tags, both HTML and PHP removed.

Code:
$handle = @fopen("webpage.html", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgetss($handle, 4096);
        echo $buffer;
    }
    fclose($handle);
}
Perhaps you would like to keep Paragraph tags, or tables intacts. TO do this, the function takes on a third parameter (string) to allow certain tags.
From our example:

Code:
change: 

    $buffer = fgetss($handle, 4096);

to 

    $buffer = fgetss($handle, 4096, "<table><tr><th><td>');

to keep tables intact
'Till next time,
WP4H
#content #fetching #file #html or php #php #quicky #stripping

Trending Topics