Posted By

evanwalsh on 06/25/08


Tagged

regex php simple String count function str sentences


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

not_skeletor
jdstraughan


Count sentences in a string


 / Published in: PHP
 

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

  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
Posted By: koncept on June 26, 2008

echo countSentences("This is a test... Hey!"); // returns 3 (oops!)

Posted By: evanwalsh on June 27, 2008

Oh my! I'll work on that

You need to login to post a comment.