Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--Write a PHP program WordMapper.php that takes a text from a textarea and prints each word
- and the number of times it occurs in the text. The search should be case-insensitive.
- The result should be printed as an HTML table.-->
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <title>Word Mapping</title>
- <style type="text/css">
- table, td {
- background-color: #eee;
- border: 1px solid #000;
- }
- </style>
- </head>
- <body>
- <form method="post">
- <textarea name="text" cols="60" rows="5" placeholder="Enter your text..."></textarea>
- <p><input type="submit" value="Count words"/></p>
- </form>
- <table>
- <tbody>
- <?php
- if (!isset($_POST) || !isset($_POST['text']) || trim($_POST['text']) === '') {
- die("The Input Form can't been Empty!!!");
- }
- $text = strtolower(trim($_POST['text']));
- $words = str_word_count($text, 1);
- $resultMap = array();
- foreach ($words as $word) {
- if (!array_key_exists($word, $resultMap)) {
- $resultMap[$word] = 1;
- } else {
- $resultMap[$word] += 1;
- }
- }
- foreach ($resultMap as $key => $number):?>
- <tr>
- <td><?= $key ?></td>
- <td><?= $number ?></td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement