0

I want to add a function to return whether the first letter is a capital or not from my last question.

Here's the code:

<?php

function isCapital($string) {
   return $string = preg_match('/[A-Z]$/',$string{0});
}

$text = " Poetry. do you read poetry while flying? Many people find it relaxing to read on long flights. Poetry can be divided into several genres, or categories. ";
$sentences = explode(".", $text); $save = array();
foreach ($sentences as $sentence) {
   if (count(preg_split('/\s+/', $sentence)) > 6) {
      $save[] = $sentence. ".";
   }
}

if( count( $save) > 0) {
   foreach ($save as $nama){
      if (isCapital($nama)){
         print_r ($nama);
      }
   }
}
?>

The result should be...

Poetry can be divided into several genres, or categories.

...but it prints nothing. I need only the sentence that consists of more than 6 words and start with capital letter.

Community
  • 1
  • 1
bruine
  • 647
  • 5
  • 16
  • Why is "Many people find it relaxing to read on long flights." not a valid sentence? – Jonathan S. Jul 02 '12 at 03:18
  • Because he's not checking on a question mark, so the sentence is read as "do you read poetry while flying? Many people find it relaxing to read on long flights." which doesn't start with a capital letter. – King Skippus Jul 02 '12 at 03:22
  • I understand that, but why would you not want to count sentences that are preceded by questions? – Jonathan S. Jul 02 '12 at 03:23
  • @georgefox oh, That's just (random) an example sentences. :D – bruine Jul 02 '12 at 03:45
  • Related: [In paragraph making the first letter of every sentence uppercase?](https://stackoverflow.com/q/46398750/2943403) and relevant: [How to check if letter is upper or lower in PHP?](https://stackoverflow.com/q/2814880/2943403) and [Split a text into sentences](https://stackoverflow.com/q/16377437/2943403) – mickmackusa May 04 '23 at 04:48

2 Answers2

4

When you do the explode() function, you are leaving a space at the start of the string, which means that the leftmost character of $string will never be a capital letter--it will be a space. I would change the isCapital() function to the following:

function isCapital($string) {
  return preg_match('/^\\s*[A-Z]/', $string) > 0;
}
King Skippus
  • 3,801
  • 1
  • 24
  • 24
  • 1
    Note that this will still leave the space on the start of your sentence, though, when you print it. To get rid of that, try something like this instead of your explode statement: `$sentences = preg_split('/\\.\\s*/', $text);` – King Skippus Jul 02 '12 at 03:18
  • Oh, and you can probably get a little fancier if you want. Might want to rule out possibilities in your regexes for stuff like Mr., Mrs., Dr., etc. Parsing text like that is actually semantically very difficult. Glad I could help, though, and good luck! – King Skippus Jul 02 '12 at 03:25
  • oh yes, feels that I need to learn about that too. Thank you so much ! sorry if my english is bad :) – bruine Jul 02 '12 at 03:39
1

You should be able to accomplish all of this through one regular expression, if you're so inclined:

preg_match_all('/((?=[A-Z])([^\s.!?]+\s+){5,}[^\s.!?]+[.!?])/', $string, $matches);

Alternatively, remove the ! and ? from the character classes to only count . as a sentence delimiter.

greg-449
  • 109,219
  • 232
  • 102
  • 145
Jonathan S.
  • 2,238
  • 16
  • 16
  • 1
    They can be daunting at first, but they're quite powerful. http://www.regular-expressions.info/ is a great resource. – Jonathan S. Jul 02 '12 at 03:39