Counting words in an array
Posted: Thu Aug 21, 2008 6:37 pm
I have some code that I cannot make work. It uses PHP to find the words in the <h1> tags in html and then counts the words. However, whenever I run it, no matter how many words are in the <h1> tags, it always returns a "1". I need help to find what is wrong with my code! Any help is appreciated!
Code: Select all
<?
$file="http://www.globalmarketingplus.com";
$h1 = get_h1($file);
$h1_words=count_words($h1);
echo "There are ".$h1_words." in h1 tags";
function get_h1($file){
$h1tags = preg_match_all("/(<h1.*>)(\w.*)(<\/h1>)/isxmU",$file,$patterns);
$res = array();
array_push($res,$patterns[2]);
array_push($res,count($patterns[2]));
return $res;
}
function count_words($str) {
$no = explode(" ",$str);
$no = strip_tags($no);
return count($no);
}
?>