PHP,
pasted
on Oct 20:
|
function str_count_sentences($string) {
// initiate a counter and define the possible sentence-ending-symbols
$i = 0;
$end_characters = array('. ', '? ', '! ');
// add a space to the end, for good mesuarement
// also: convert /r, /n etc to /rSPACE and /nSPACE
$string = $string . ' ';
$string = str_replace(PHP_EOL, PHP_EOL.' ', $string);
// for each end-character: count how many there are.
foreach ($end_characters as $end_character) {
// count them
$i_by_end_character = substr_count($string, $end_character);
// sum up the countings
$counter_sentences += $i_by_end_character;
}
// return a INT of the amount of sentence-enders (=== sentences)
return $counter_sentences;
}
|
Output:
|
function str_count_sentences($string) {
// initiate a counter and define the possible sentence-ending-symbols
$i = 0;
$end_characters = array('. ', '? ', '! ');
// add a space to the end, for good mesuarement
// also: convert /r, /n etc to /rSPACE and /nSPACE
$string = $string . ' ';
$string = str_replace(PHP_EOL, PHP_EOL.' ', $string);
// for each end-character: count how many there are.
foreach ($end_characters as $end_character) {
// count them
$i_by_end_character = substr_count($string, $end_character);
// sum up the countings
$counter_sentences += $i_by_end_character;
}
// return a INT of the amount of sentence-enders (=== sentences)
return $counter_sentences;
}
|
|