Counting Characters in PHP


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. $f = "test.txt";
  3.  
  4. // get string
  5. $str = file_get_contents($f);
  6.  
  7. // count characters
  8. $numChar = strlen($str);
  9. echo "This file has ". $numChar . " character(s)";
  10.  
  11. // count characters without spaces
  12. $str2 = ereg_replace('[[:space:]]+', '', $str);
  13. $numChar = strlen($str2);
  14. echo "This file has ". $numChar . " character(s) without spaces";
  15.  
  16. // count words
  17. $numWords = str_word_count($str);
  18. echo "This file has ". $numWords . " words";
  19.  
  20. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.