fork(1) download
  1. <?php
  2.  
  3. $re = '/# Split sentences on whitespace between them.
  4. (?<= # Begin positive lookbehind.
  5. [.!?] # Either an end of sentence punct,
  6. | [.!?][\'"] # or end of sentence punct and quote.
  7. ) # End positive lookbehind.
  8. (?<! # Begin negative lookbehind.
  9. Mr\. # Skip either "Mr."
  10. | Mrs\. # or "Mrs.",
  11. | Ms\. # or "Ms.",
  12. | Jr\. # or "Jr.",
  13. | Dr\. # or "Dr.",
  14. | Prof\. # or "Prof.",
  15. | Sr\. # or "Sr.",
  16. | T\.V\.A\. # or "T.V.A.",
  17. # or... (you get the idea).
  18. ) # End negative lookbehind.
  19. (?:\h+|^$) # Split on whitespace between sentences\/empty lines.
  20. /mix';
  21.  
  22. $text = <<<EOL
  23. This is paragraph one. This is sentence one. Sentence two!
  24.  
  25. This is paragraph two. This is sentence three. Sentence four!
  26. EOL;
  27.  
  28. echo "\nBefore: \n" . $text . "\n";
  29.  
  30. $sentences = preg_split($re, $text, -1);
  31.  
  32. $sentences[1] = " "; // remove 'sentence one'
  33.  
  34. // put text back together
  35. $text = implode( $sentences );
  36.  
  37. echo "\nAfter: \n" . $text . "\n";
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
Before: 
This is paragraph one. This is sentence one. Sentence two!

This is paragraph two. This is sentence three. Sentence four!

After: 
This is paragraph one. Sentence two!

This is paragraph two.This is sentence three.Sentence four!