Filtering javascript from PHP site content

So I'm making a script to check the keyword density on a page based on the url the user is submitting and I used strip_tags but it doesn't seem to completely filter the javascript and other code from the actual word content on the site. Is there a better way to filter between the content of the code on the page and the actual content of the word?

if(isset($_POST['url'])){
$url = $_POST['url'];
$str = strip_tags(file_get_contents($url));
$words      = str_word_count(strtolower($str),1);
$word_count = array_count_values($words);

foreach ($word_count as $key=>$val) {
    $density = ($val/count($words))*100;
        echo "$key - COUNT: $val, DENSITY: ".number_format($density,2)."%<br/>\n";
}
}

      

+3


source to share


2 answers


I wrote two functions for this:

/**
 * Removes all Tags provided from an Html string
 *
 * @param string   $str    The Html String
 * @param string[] $tagArr An Array with all Tag Names to be removed
 *
 * @return string The Html String without the tags
 */
function removeTags($str, $tagArr)
{
    foreach ($tagArr as $tag) {
        $str = preg_replace('#<' . $tag . '(.*?)>(.*?)</' . $tag . '>#is', '', $str);
    }
    return $str;
}

/**
 * cleans some html string
 *
 * @param string $str some html string
 *
 * @return string the cleaned string
 */
function filterHtml($str)
{
    //Remove Tags
    $str = removeTags($str, ['script', 'style']);

    //Remove all Tags, but not the Content
    $str = preg_replace('/<[^>]*>/', ' ', $str);

    //Remove Linebreaks and Tabs
    $str = str_replace(["\n", "\t", "\r"], ' ', $str);

    //Remove Double Whitespace
    while (strpos($str, '  ') !== false) {
        $str = str_replace('  ', ' ', $str);
    }

    //Return trimmed
    return trim($str);
}

      



Working example

$fileContent     = file_get_contents('http://stackoverflow.com/questions/25537377/filtering-html-from-site-content-php');
$filteredContent = filterHtml($fileContent);
var_dump($filteredContent);

      

0


source


You need to parse the HTML so that you have a similar DOM structure that you can iterate over and access the content of various nodes.



You can use PHP Simple HTML DOM Parser

0


source







All Articles