Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to count words (and characters)

Status
Not open for further replies.

aas1611

Programmer
Dec 14, 2001
184
DE
Hi,

Is there such a function that counts words and/or characters from a content of a variable?

For example I have a variable:

$text ="Have a nice day."
(of course, the actual content comes from a MySQL field).

The word count function would return 4, and the character count function would return 15.

Or is there another way to do this? in MySQL perhaps?

Thanks for all the hints.

Andre
 
Look up [blue]strlen()[/blue]

and [blue]str_word_count()[/blue]

in PHP's online manual. at php.net.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Here is the problem:

The text I want to count is originally an HTML text. I want to count the characters without the HTML tags. That's why I have to get rid of the '<' and '>' first. But it still doesn't work.

For example, here is my PHP code:

Code:
$Html = "<p>Have a nice day</p>";

// this function is to strip the HTML tags away
function parse2($Html) {
   while ($startpoint = strpos($Html, "<", 0)):
      $stop = strpos($Html, ">", $startpoint) + 1;
      $Html = substr($Html,0, $startpoint) . substr ($Html, $stop);
    endwhile;
    return $Html;
}
$parsedtext4= parse2($Html);

echo $parsedtext4;
echo "<br>";
echo strlen($parsedtext4);

At echo $parsedtext4, it returns: Have a nice day
At echo strlen($parsedtext4), it returns: 22 (instead of 15), because obviously it counts the tags also.

Is there a way to count the string without the HTML tags?

Please help!! Thanks!

Andre
 
your while loop is incorrectly phrased. try this instead

Code:
while (($startpoint = strpos($Html, "<", 0)) !== FALSE):
 
Hello again jpadie!

it works now, with the new "while" you just gave me. But the while loop here
Code:
while ($startpoint = strpos($Html, "<", 0)):
you also gave me in thread434-1193702.

So what is the difference?

Vragabond,
strip_tags() also looks promising, I've read the explanation from php.net. Haven't tried it, though, since it already works, thanks to jpadie.

Thanks!

Andre
 
curious! and embarrassing

the problem was the equals sign. strpos was returning a zero as it found the needle at position zero. thus was failing immediately.

the revised code above is better as it properly tests for FALSE rather than zero.

sorry ...
 
I see...


So, !== FALSE means TRUE, is that right?

Double negative always confuses me sometimes...

Thanks again for your help!


Andre
 
So, !== FALSE means TRUE, is that right?

umm ... no! !== FALSE can mean true but it can also mean anything that is not boolean and equal to false. eg an alphanumeric, a numeral, boolean true, an array, an object etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top