runeveryday Posted March 11, 2010 Share Posted March 11, 2010 I am working on a project where I have to find out the keyword density of the page on the basis of URL of that page,But I am not aware actually how to begin? and also how can we create a PHP script which will fetch the keyword density of a web page.who can give me a train of thought.the more detail,the better.thanks in advance Link to comment https://forums.phpfreaks.com/topic/194903-keyword-density-of-the-page/ Share on other sites More sharing options...
runeveryday Posted March 12, 2010 Author Share Posted March 12, 2010 anyone can help me ? Link to comment https://forums.phpfreaks.com/topic/194903-keyword-density-of-the-page/#findComment-1025010 Share on other sites More sharing options...
teamatomic Posted March 12, 2010 Share Posted March 12, 2010 Use file_get_contents to get the page then use get_meta_tags to get the keywords then grab the body grab the links text break the body down to just words count the words loop through and count keyword occurrences in body text count occurrences of keywords in link text divide to get the percentage or however else you want to weigh it HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/194903-keyword-density-of-the-page/#findComment-1025024 Share on other sites More sharing options...
runeveryday Posted March 12, 2010 Author Share Posted March 12, 2010 thank you very much,but i still don't know how to grab the body, Link to comment https://forums.phpfreaks.com/topic/194903-keyword-density-of-the-page/#findComment-1025056 Share on other sites More sharing options...
runeveryday Posted March 12, 2010 Author Share Posted March 12, 2010 i am a newbie,the following code is i tried to do about the problem.hope anyone can help me. the primary front code: <form action="check.php" method="post"> Keyword:<input type="text" name="keyword" /> Website URL: <input type="text" name="url" /> <input type="check" /> </form> php file: <?php $homepage = file_get_contents('$_POST["url"]'); $tags = get_meta_tags('$_POST["url"]'); ......... ?> i am sorry,i don't know how to continue. and i want to the keywords which the vistor can input. Link to comment https://forums.phpfreaks.com/topic/194903-keyword-density-of-the-page/#findComment-1025064 Share on other sites More sharing options...
teamatomic Posted March 12, 2010 Share Posted March 12, 2010 This is from an ugly little script we use internally for SEO. //define regex for preg $b_start = '<body .*>'; $b_stop = '<\/body>'; //match the content between tags preg_match( "/$b_start(.*)$b_stop/s", $content, $body ); // grab body text $body=$body[0]; // get links text from body preg_match_all('/<a .*\>(.*)<\/a>/', $body, $link_text); $lkw=$link_text[1]; array_filter($lkw); //strip out html $bt=strip_tags($body); HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/194903-keyword-density-of-the-page/#findComment-1025066 Share on other sites More sharing options...
teamatomic Posted March 12, 2010 Share Posted March 12, 2010 Here, here's the whole thingy. Do anything you want with it except say you own it. Just set the url and that it. $url = "put url here"; $content=file_get_contents("$url"); $start = '<title>'; $end = '<\/title>'; preg_match( "/$start(.*)$end/s", $content, $match ); $title = $match[1]; $metatagarray = get_meta_tags("$url"); $keywords = $metatagarray["keywords"]; $keywords_array=explode(",",$keywords); $description = $metatagarray["description"]; echo "<b>URL ::</b> $url<br>"; echo "<b>Title ::</b> $title</br>"; echo "<b>Description ::</b> $description<br>"; echo "<b>Keywords ::</b> $keywords<br>"; $b_start = '<body .*>'; $b_stop = '<\/body>'; preg_match( "/$b_start(.*)$b_stop/s", $content, $body ); $body=$body[0]; $bt=strip_tags($body); $body_word_count = count(explode(" ",$bt)); echo "<br><b>Body Word Count</b> - $body_word_count<br>"; preg_match_all('/<a .*\>(.*)<\/a>/', $body, $link_text); $lkw=$link_text[1]; array_filter($lkw); $kw_found=array(); foreach($keywords_array as $word) { $i=1; $word=strtolower($word); foreach($lkw as $w) { $w=strtolower($w); if($word == $w) { $kw_found[$w]=$i; $i++; } } } echo "<p><b>Keyword Link Match</b><br>"; foreach($kw_found as $key => $value) { $p1 = floatval($value) / floatval($body_word_count); $p2 = $p1 * 100; $p=sprintf("%01.2f", $p2); echo "$key :: $value :: $p%<br>"; } echo "<p>"; $body_count=array(); foreach($keywords_array as $word) { $count = substr_count($bt,$word); $body_count[$word]=$count; $count=0; } echo "<p><b>Keyword Body Match</b><br>"; foreach($body_count as $key => $value) { $p1 = floatval($value) / floatval($body_word_count); $p2 = $p1 * 100; $p=sprintf("%01.2f", $p2); echo "$key :: $value :: $p%<br>"; } HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/194903-keyword-density-of-the-page/#findComment-1025068 Share on other sites More sharing options...
runeveryday Posted March 12, 2010 Author Share Posted March 12, 2010 thanks for share, and many many thanks~ Link to comment https://forums.phpfreaks.com/topic/194903-keyword-density-of-the-page/#findComment-1025073 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.