Advertisement
dimipan80

Sentence Extractor

Apr 24th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. <!--Write a PHP program SentenceExtractor.php that takes a text from a textarea and a word from an input field
  2. and prints all sentences from the text, containing that word. A sentence is any sequence of words ending
  3. with ., ! or ?.-->
  4.  
  5. <!DOCTYPE html>
  6. <html>
  7. <head lang="en">
  8.     <meta charset="UTF-8">
  9.     <title>Sentence Extractor</title>
  10.     <style type="text/css">
  11.         #container {
  12.             width: 340px;
  13.         }
  14.  
  15.         input[type="text"] {
  16.             width: 200px;
  17.             text-indent: 2px;
  18.         }
  19.     </style>
  20. </head>
  21. <body>
  22. <div id="container">
  23.     <form method="post">
  24.         <p><textarea name="text" cols="50" rows="8" placeholder="Enter your text..."
  25.                      autofocus required></textarea></p>
  26.  
  27.         <p><label for="word">Enter word to check:</label>
  28.             <input type="text" name="word" id="word" required/></p>
  29.         <input type="submit" value="Extract sentence"/>
  30.     </form>
  31.     <p>
  32.         <?php
  33.         mb_internal_encoding('UTF-8');
  34.  
  35.         if (!isset($_POST['text']) || !isset($_POST['word']) ||
  36.             trim($_POST['text']) === '' || trim($_POST['word']) === ''
  37.         ) {
  38.             die('Please, enter text in Each Textfield on Input Form!!!');
  39.         }
  40.  
  41.         $text = trim($_POST['text']);
  42.         $contained_word = trim($_POST['word']);
  43.         $word_length = strlen($contained_word);
  44.         $text_words = str_word_count($text, 2);
  45.         if (in_array($contained_word, $text_words)) {
  46.             $start_index = 0;
  47.             foreach ($text_words as $word_position => $word) {
  48.                 if ($word === $contained_word) {
  49.                     $dot_index = stripos($text, '.', ($word_position + $word_length));
  50.                     $exclamation_index = stripos($text, '!', ($word_position + $word_length));
  51.                     $question_index = stripos($text, '?', ($word_position + $word_length));
  52.                     if ($dot_index === false && $exclamation_index === false && $question_index === false) {
  53.                         break;
  54.                     }
  55.  
  56.                     $end_sentence = strlen($text) + 1;
  57.                     if ($dot_index !== false && $dot_index < $end_sentence) {
  58.                         $end_sentence = $dot_index;
  59.                     }
  60.                     if ($exclamation_index !== false && $exclamation_index < $end_sentence) {
  61.                         $end_sentence = $exclamation_index;
  62.                     }
  63.                     if ($question_index !== false && $question_index < $end_sentence) {
  64.                         $end_sentence = $question_index;
  65.                     }
  66.  
  67.                     if ($end_sentence > $word_position) {
  68.                         echo substr($text, $start_index, ($end_sentence - $start_index + 1)) . ' ';
  69.                     }
  70.  
  71.                     $start_index = $end_sentence + 1;
  72.                 }
  73.             }
  74.         }      
  75.         ?>
  76.     </p>
  77. </div>
  78. </body>
  79. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement