Count sentences in a string


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

This will count sentences that end in . or ? or ! and have a space after them.


Copy this code and paste it in your HTML
  1. function countSentences($str){
  2. return preg_match_all('/[^\s](\.|\!|\?)(?!\w)/',$str,$match);
  3. }
  4.  
  5. countSentences("This is a test. Hey!"); // returns 2
  6. countSentences("What do you want?"); // returns 1
  7. // etc.

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.