utahfriend Posted August 22, 2008 Share Posted August 22, 2008 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! <? $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); } ?> Link to comment https://forums.phpfreaks.com/topic/120786-counting-words-in-an-array/ Share on other sites More sharing options...
teng84 Posted August 22, 2008 Share Posted August 22, 2008 function count_words($str) { $no = explode(" ",strip_tags($str)); return count($no); } try ... Link to comment https://forums.phpfreaks.com/topic/120786-counting-words-in-an-array/#findComment-622609 Share on other sites More sharing options...
utahfriend Posted August 22, 2008 Author Share Posted August 22, 2008 Still get the same results. the result should be 14 but I still get 1 Link to comment https://forums.phpfreaks.com/topic/120786-counting-words-in-an-array/#findComment-622633 Share on other sites More sharing options...
utahfriend Posted August 22, 2008 Author Share Posted August 22, 2008 I added this to display the words to the function, but it returns "array" instead of the words: function count_words($str) { $no = explode(" ",strip_tags($str)); foreach($no as $c) { echo "h1: $c<br>"; } return count($no); } Link to comment https://forums.phpfreaks.com/topic/120786-counting-words-in-an-array/#findComment-622635 Share on other sites More sharing options...
JasonLewis Posted August 22, 2008 Share Posted August 22, 2008 Does $h1 return all the words inside the h1 tags? Use print_r on the arrays, and see how they have been constructed. Link to comment https://forums.phpfreaks.com/topic/120786-counting-words-in-an-array/#findComment-622679 Share on other sites More sharing options...
utahfriend Posted August 22, 2008 Author Share Posted August 22, 2008 Using the following code will display all the words in the h1 tags echo"<h1>Content properties</h1>"; // get h1 tags if($h1[1] != 0){ echo "<br/>H1 Tags found: $h1[1]<ul>"; foreach($h1[0] as $key => $val){ echo "<li>" . htmlentities($val) . "</li>"; } echo "</ul>"; }else{ echo "<br/><div class=\"error\">No H1 Tags found</div><br/>"; } When I did the print_r I got this result: Array ( [0] => Array ( ) [1] => 0 ) Link to comment https://forums.phpfreaks.com/topic/120786-counting-words-in-an-array/#findComment-622686 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.