View Full Version : word counter...
evil-ug
12-17-2002, 08:05 PM
Hi people,
I need to make a word counter in PHP, I have one in ASP and other in Java Script, but in PHP I don't know how to make it...
The thing is simple... I wrote "Hello World".. and the script have to tell me "you wrote 2 words"
any help...
thanks in advance.
Spookster
12-17-2002, 09:58 PM
It's not too difficult. Just use the same logic/algorithm as the ones you wrote in Javascript and ASP. What's this for by the way?
$cnt = explode(" ",$text);
echo count($cnt);
As spooks says, using a common algorythm that you can port between languages is a better idea
Spookster
12-17-2002, 11:32 PM
Well it might be a good idea to clean up the string first as well. This is one I had written awhile back:
<html>
<head>
</head>
<body>
<form name="foo" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<textarea name="input" cols="50" rows="6" wrap="VIRTUAL" id="input"></textarea>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
$input = $_POST["input"];
function count_words($string) {
// Remove line breaks, double spaces, etc from the string
// You can add to this section to clean up the string
// before attempting to count the number of words.
$string = str_replace("\n"," ",$string);
$string = str_replace(" "," ",$string);
// Split the string into an array of words.
// It is set to split when a single space is found
$word_array = explode(" ", $string);
// Counts the number of elements in the array
// which should be about how many words were in the string
$num_of_words = count($word_array);
return $num_of_words;
}
echo "<br><br>" . count_words($input);
?>
</body>
</html>
evil-ug
12-18-2002, 12:18 PM
thanks fellows!!!!!
it works like heaven, thank you very much...
cya.
PD: I really have to learn php...
feldmann-gbr
02-25-2007, 05:26 PM
Hello,
could you quote me about a more complicated word count script with a functionality like the one below, please? I would perhaps like to use it for my own translation business which I am going to launch soon. The script should run within the Joomla 1.0.12 CMS and be able to process western and eastern European languages (e.g. Czech, Polish, Hungarian), calculate the cost of the text to be translated and send the results to me via e-mail.
http://www.translation-probst.ch/d/auftrag.php
Michael
marek_mar
02-25-2007, 06:30 PM
... and I'd do it this way:
<?php
$string = 'some words go here';
print '$string has ' . str_word_count($string) . ' words.';
?>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.