GochiSiyan

custom word count

Mar 8th, 2022
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. function jnews_count_words( $str ) {
  2. $OUT = 0;
  3. $IN = 1;
  4. $state = $OUT;
  5. $wc = 0; // word count
  6. $i = 0;
  7.  
  8. // Scan all characters one by one
  9. while ($i < strlen($str))
  10. {
  11. // If next character is
  12. // a separator, set the
  13. // state as OUT
  14. if ($str[$i] == " " ||
  15. $str[$i] == "\n" ||
  16. $str[$i] == "\t")
  17. $state = $OUT;
  18.  
  19. // If next character is not a
  20. // word separator and state is
  21. // OUT, then set the state as
  22. // IN and increment word count
  23. else if ($state == $OUT)
  24. {
  25. $state = $IN;
  26. ++$wc;
  27. }
  28.  
  29. // Move to next character
  30. ++$i;
  31. }
  32.  
  33. return $wc;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment