All pastes #506796 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-05-24 05:56 UTC
rendered paste body
<?php
function strip_html($data)
{
    // strip unecessary comments and characters from a webpages text
    // all line comments, multi-line comments \\r \\n \\t multi-spaces that make a script readable.
    // it also safeguards enquoted values and values within textareas, as these are required

    $data=preg_replace_callback("/>[^<]*<\\/textarea/i", "harden_characters", $data);
    $data=preg_replace_callback('/\\"[^"<>]+\\"/', "harden_characters", $data);

    $data=preg_replace("/(\/\/.*n)/","",$data); // remove single line comments, like this, from // to \\n
    $data=preg_replace("/(t|r|n)/","",$data);  // remove new lines \\n, tabs and \\r
    $data=preg_replace("/(\/*.*\*\/)/","",$data);  // remove multi-line comments /* */
    $data=preg_replace("/(<![^>]*>)/","",$data);  // remove multi-line comments <!-- -->
    $data=preg_replace('/(\\s+)/', ' ',$data); // replace multi spaces with singles
    $data=preg_replace('/>\\s</', '><',$data);

    $data=preg_replace_callback('/"[^\\"<>]+"/', "unharden_characters", $data);
    $data=preg_replace_callback("/>[^<]*<\\/textarea/", "unharden_characters", $data);

    return $data;
}

function harden_characters($array)
{
    $safe=$array[0];
    $safe=preg_replace('/\\n/', "%0A", $safe);
    $safe=preg_replace('/\\t/', "%09", $safe);
    $safe=preg_replace('/\\s/', "&nbsp;", $safe);
    return $safe;
}
function unharden_characters($array)
{
    $safe=$array[0];
    $safe=preg_replace('/%0A/', "\\n", $safe);
    $safe=preg_replace('/%09/', "\\t", $safe);
    $safe=preg_replace('/&nbsp;/', " ", $safe);
    return $safe;
}

 echo strip_html(file_get_contents("php://stdin"));

?>