Jump to content

substr_count() whole words in HTML


gerkintrigg

Recommended Posts

Hi everyone.

 

I'm trying to count how many instances of specific words are in a piece of code (with the tags removed).

 

I used this code:

$plus=substr_count(strip_tags($page), $r['word']); 

but the issue is that it picks up words like "sealing" as containing words like "sea".

 

I looked for it on the net and the closest I found was this:

 

if ((strlen($body) > 180) && (strlen($body) > 1)) { 
$whitespaceposition = strpos($body," ",175)-1; 
$body = substr($body, 0, $whitespaceposition); 
}

The only issue is that once I strip the tags, there may not be any white space before / after a word, so I'm not convinced it'll work as I want it to.

 

Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/183340-substr_count-whole-words-in-html/
Share on other sites

Are you using exactly that code? If so, that makes no sense at all since the regex looks for a word but you only supply it with that word in the subject parameter and provide a string for what should be a by-ref variable. 

 

From the looks of things, you probably want more like:

 

$plus = preg_match_all('/\b'.preg_quote($r['word'], '/').'\b/', strip_tags($my_page));

I just came across str_word_count.

 

Is there a way of using that?

 

I have tried:

 

<?php
$txt = "Text Text Web Web Text";
$words = str_word_count($summary);
   echo "Total words in summary: $words";
?>

this outputs "5".

 

Is there a way of passing other parameters to this function to only match words like "Web" so that it outputs "2"?

 

Or does that need to be a more complex regex issue?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.