Here’s a Word Counter Tool using PHP. This tool allows users to input text and calculate the number of words and characters in the input.

1. PHP Code for Word Counter Tool

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Word Counter Tool to count words and characters in a text.">
    <meta name="keywords" content="Word Counter, PHP, Text Tool, Character Counter">
    <title>Word Counter Tool</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
        }
        form {
            margin-top: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        textarea {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            height: 200px;
        }
        input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            cursor: pointer;
        }
        .result {
            font-size: 18px;
            text-align: center;
            color: #333;
        }
        .error {
            color: red;
            text-align: center;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>Word Counter Tool</h1>
    <p>Enter your text to count the number of words and characters.</p>

    <form method="post">
        <label for="text">Enter your text:</label>
        <textarea id="text" name="text" placeholder="Enter or paste your text here..." required></textarea>
        <input type="submit" name="count_words" value="Count Words & Characters">
    </form>

    <?php
    if (isset($_POST['count_words'])) {
        $text = trim($_POST['text']);

        if (!empty($text)) {
            // Count the number of words
            $word_count = str_word_count($text);

            // Count the number of characters (excluding spaces)
            $char_count = strlen(str_replace(' ', '', $text));

            echo "<div class='result'>";
            echo "<p><strong>Word Count:</strong> $word_count</p>";
            echo "<p><strong>Character Count (excluding spaces):</strong> $char_count</p>";
            echo "</div>";
        } else {
            echo "<div class='error'>Please enter some text to count.</div>";
        }
    }
    ?>
</div>

</body>
</html>

2. How the Word Counter Tool Works

  • Input:
  • Users enter text into the textarea.
  • Processing:
  • The PHP script counts the number of words using str_word_count() and the number of characters (excluding spaces) using strlen() combined with str_replace() to remove spaces.
  • Output:
  • Displays the total word count and character count (excluding spaces).
See also  how to create & design To-Do List Manager ?

3. Integrating the Word Counter Tool into WordPress

Method 1: Creating a Custom Plugin

Steps:
  1. Create a Plugin Folder:
  • Go to /wp-content/plugins/ and create a folder called word-counter-tool.
  1. Create the Plugin File:
  • Inside the word-counter-tool folder, create a file named word-counter.php.
  • Add the following PHP code:
<?php
/*
Plugin Name: Word Counter Tool
Description: A simple tool to count the number of words and characters in a given text.
Version: 1.0
Author: Your Name
*/

// Function to display the Word Counter Tool
function word_counter_tool_shortcode() {
    ob_start(); // Start output buffering
    ?>

    <div class="container">
        <h1>Word Counter Tool</h1>
        <p>Enter your text to count the number of words and characters.</p>

        <form method="post">
            <label for="text">Enter your text:</label>
            <textarea id="text" name="text" placeholder="Enter or paste your text here..." required></textarea>
            <input type="submit" name="count_words" value="Count Words & Characters">
        </form>

        <?php
        if (isset($_POST['count_words'])) {
            $text = trim($_POST['text']);

            if (!empty($text)) {
                // Count the number of words
                $word_count = str_word_count($text);

                // Count the number of characters (excluding spaces)
                $char_count = strlen(str_replace(' ', '', $text));

                echo "<div class='result'>";
                echo "<p><strong>Word Count:</strong> $word_count</p>";
                echo "<p><strong>Character Count (excluding spaces):</strong> $char_count</p>";
                echo "</div>";
            } else {
                echo "<div class='error'>Please enter some text to count.</div>";
            }
        }
        ?>
    </div>

    <?php
    return ob_get_clean(); // Return the output
}

// Register the shortcode [word_counter_tool]
add_shortcode('word_counter_tool', 'word_counter_tool_shortcode');
?>
  1. Activate the Plugin:
  • Go to the WordPress Admin Dashboard > Plugins and activate the Word Counter Tool plugin.
  1. Add the Shortcode:
  • To display the tool on any post or page, use the shortcode:
   [word_counter_tool]

Method 2: Using a PHP Code Execution Plugin

  1. Install a PHP Code Plugin:
  • Install and activate the “Insert PHP Code Snippet” plugin (or similar).
  1. Add the Word Counter Code:
  • Add the PHP code as a snippet.
  1. Use the Shortcode:
  • The snippet will generate a shortcode, such as [insert_php_snippet id="1"].
  • Insert this shortcode in any post or page to display the tool.
See also  How to create & design Unit Converter Tool ?

This Word Counter Tool is a useful feature for bloggers, writers, or anyone looking to quickly count the words and characters in their text.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *