mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 07:08:13 +01:00
simple function for count sentence
This commit is contained in:
parent
84e55a0aa7
commit
f9f005920b
23
String/CountSentences.php
Normal file
23
String/CountSentences.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This is a simple way to count sentence
|
||||
* using php preg_match_all() function
|
||||
* @param string $sentence
|
||||
*/
|
||||
|
||||
function countSentences(string $sentence): int
|
||||
{
|
||||
$sentence = trim($sentence);
|
||||
|
||||
return preg_match_all('/[^\s|^\...](\.|\!|\?)(?!\w)/', $sentence);
|
||||
}
|
||||
|
||||
|
||||
// example
|
||||
echo countSentences("Hi! Hello world."); // returns 2
|
||||
echo PHP_EOL;
|
||||
echo countSentences("i am testing. test...."); // returns 2
|
||||
echo PHP_EOL;
|
||||
echo countSentences("How are you?"); // returns 1
|
||||
echo PHP_EOL;
|
Loading…
x
Reference in New Issue
Block a user