September 27, 201015 yr I fudged this script together to check a websites body content and generate a message whether a keyword is present more than twice. only it breaks and doesn't work if the "keyword" is more than one word. How to I make it work so it can cater for multiple words within the "keyword" <?php $keyword = ($_POST['keyword']); // defined by form $domainname = ($_POST['url']); // defined by form $file = file_get_contents($domainname); $body = preg_replace("/.*<body[^>]*>|<\/body>.*/si", "", $file);{ $bodyp = strip_tags($file); if (substr_count(strtolower($bodyp), strtolower($keyword)) > 2 ){ echo "<span class='good'>Yes. </span>Awesome, your keyword is present in the body of your content."; } else { echo "<span class='bad'>No. </span>You should really have the keyword your looking for in content of your website."; } } ?> Cheers people ()
September 27, 201015 yr Personally I'd strip all HTML tags using strip_tags() then compare that string using strpos()
September 27, 201015 yr Author Personally I'd strip all HTML tags using strip_tags() then compare that string using strpos() I have striped all html tags... $bodyp = strip_tags($file); When I echo out the result ($bodyp) I get text..which has the two word keyword in, but it tell me that its not there.
September 27, 201015 yr I dont know much about substr_count but I suggested using strpos as an alternative, for example... <?php $keyword = ($_POST['keyword']); // defined by form $domainname = ($_POST['url']); // defined by form //tests $domainname = 'http://news.bbc.co.uk/sport1/hi/football/scot_prem/9028899.stm'; $keyword = "kenny miller"; function keyWordCount($keyword, $body) { $found = $offset = 0; while(!(strpos($body, $keyword, $offset) === false)) { $found++; $offset = strpos($body, $keyword, $offset)+1; } return $found; } $file = file_get_contents($domainname); $body = preg_replace("/.*<body[^>]*>|<\/body>.*/si", "", $file); //this is EXTREMELY CPU intensive! $bodyp = strip_tags($body); if (keyWordCount(strtolower($keyword), strtolower($bodyp)) > 2 ) { echo "<span class='good'>Yes. </span>Awesome, your keyword is present in the body of your content."; } else { echo "<span class='bad'>No. </span>You should really have the keyword your looking for in content of your website."; } ?>
Create an account or sign in to comment