Jump to content

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

like this?

$plus=preg_match_all('~\b'.$r['word'].'\b(?![^<]*?>)~', $r['word'], strip_tags($my_page));

 

It just seems to give a very very high number... Usually 285 regardless of whether I check 5 words or 2...

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.