Give Me Cake

CakePHP Baking, Internet Marketing, SEO, Design and more

Max no of characters, max no of words

Posted on | June 9, 2009 | No Comments

Remember if you still remember those school days, when you have to write compositions, you need to mark out exactly 350 words. The purpose is for the marker to know where he or she can stop reading.

PHP functions that does word count

str_word_count()

And on the str_word_count() manual page, these is this helpful function for UTF8 word count
define("WORD_COUNT_MASK", "/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]*/u");

function str_word_count_utf8($string, $format = 0)
{
switch (
$format) {
case
1:
preg_match_all(WORD_COUNT_MASK, $string, $matches);
return
$matches[0];
case
2:
preg_match_all(WORD_COUNT_MASK, $string, $matches, PREG_OFFSET_CAPTURE);
$result = array();
foreach (
$matches[0] as $match) {
$result[$match[1]] = $match[0];
}
return
$result;
}
return
preg_match_all(WORD_COUNT_MASK, $string, $matches);
}

Do rememebr to strip off all html tags first before you do the word count.

No TweetBacks yet. (bookmark

Comments

Leave a Reply





  • Give me Cake?

    Givemecake.dev-work.com a collection of web application development tips, tutorials and resources.
  • See Your Ads Here

  • See Your Ads Here

  • Meta