Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--Write a PHP program SentenceExtractor.php that takes a text from a textarea and a word from an input field
- and prints all sentences from the text, containing that word. A sentence is any sequence of words ending
- with ., ! or ?.-->
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <title>Sentence Extractor</title>
- <style type="text/css">
- #container {
- width: 340px;
- }
- input[type="text"] {
- width: 200px;
- text-indent: 2px;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <form method="post">
- <p><textarea name="text" cols="50" rows="8" placeholder="Enter your text..."
- autofocus required></textarea></p>
- <p><label for="word">Enter word to check:</label>
- <input type="text" name="word" id="word" required/></p>
- <input type="submit" value="Extract sentence"/>
- </form>
- <p>
- <?php
- mb_internal_encoding('UTF-8');
- if (!isset($_POST['text']) || !isset($_POST['word']) ||
- trim($_POST['text']) === '' || trim($_POST['word']) === ''
- ) {
- die('Please, enter text in Each Textfield on Input Form!!!');
- }
- $text = trim($_POST['text']);
- $contained_word = trim($_POST['word']);
- $word_length = strlen($contained_word);
- $text_words = str_word_count($text, 2);
- if (in_array($contained_word, $text_words)) {
- $start_index = 0;
- foreach ($text_words as $word_position => $word) {
- if ($word === $contained_word) {
- $dot_index = stripos($text, '.', ($word_position + $word_length));
- $exclamation_index = stripos($text, '!', ($word_position + $word_length));
- $question_index = stripos($text, '?', ($word_position + $word_length));
- if ($dot_index === false && $exclamation_index === false && $question_index === false) {
- break;
- }
- $end_sentence = strlen($text) + 1;
- if ($dot_index !== false && $dot_index < $end_sentence) {
- $end_sentence = $dot_index;
- }
- if ($exclamation_index !== false && $exclamation_index < $end_sentence) {
- $end_sentence = $exclamation_index;
- }
- if ($question_index !== false && $question_index < $end_sentence) {
- $end_sentence = $question_index;
- }
- if ($end_sentence > $word_position) {
- echo substr($text, $start_index, ($end_sentence - $start_index + 1)) . ' ';
- }
- $start_index = $end_sentence + 1;
- }
- }
- }
- ?>
- </p>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement