Project: Text Analyzer

Time to build something practical with your string function skills. You'll create a text analyzer that processes user input and provides detailed statistics about their writing. This project reinforces string manipulation while solving real problems writers and content creators face daily.

Text analysis tools help writers understand their content's complexity, readability, and structure. Your analyzer will break down text into measurable components and present actionable insights about writing style and composition.

Assignment

Create a PHP application that accepts text input from users and generates a comprehensive analysis report. The application should demonstrate advanced string function usage while providing genuinely useful feedback about text characteristics.

Your text analyzer must process raw text input and transform it into meaningful statistics that help users understand their writing patterns.

Learning Objectives

By completing this project, you'll demonstrate your ability to:

Requirements

Your text analyzer must include these specific features:

Basic Counting (Required)

Simple Analysis (Required)

String Function Requirements

Input Processing

Output Format

Step-by-Step Approach

Build this analyzer incrementally to avoid overwhelming complexity.

Step 1: Create the Input Form

Start with a simple form that accepts text input. Include clear instructions about what users should enter.

Step 2: Count Characters and Words

Implement basic counting using strlen() and explode(). Start with simple word splitting on spaces.

Step 3: Count Sentences

Use substr_count() or string replacement to count punctuation marks that end sentences.

Step 4: Find the Longest Word

Use explode() to get word array, then loop through to find the longest one.

Step 5: Create Word Frequency List

Convert words to lowercase and count occurrences. Show the top 3 most common words.

Step 6: Calculate and Display Results

Format everything nicely and handle cases where users enter very short text.

Technical Implementation Guidelines

Text Cleaning: Use trim() to remove extra whitespace before analysis. Replace multiple spaces with single spaces for consistent word counting.

Word Splitting: Use explode(' ', $text) to break text into words. Filter out empty array elements that result from multiple spaces.

Sentence Counting: Count periods, exclamation marks, and question marks. You can use substr_count() three times and add the results.

Case Handling: Convert text to lowercase with strtolower() before counting word frequency to treat "The" and "the" as the same word.

Simple Validation: Check if the cleaned input is empty and show a friendly error message instead of trying to analyze nothing.

Example Analysis Output

Your completed analyzer should produce output similar to this structure (but use your own formatting):

Text Analysis Results
=====================

Basic Statistics:
- Characters (with spaces): 487
- Characters (without spaces): 392
- Words: 89
- Sentences: 7

Analysis:
- Longest word: "comprehensive" (13 characters)
- Average word length: 4.4 characters
- Most common words: writing (4), content (3), analysis (3)

Don't copy this exact format. Design your own output structure that presents information clearly and logically.

Common Implementation Challenges

Accurate Word Counting: When using explode(' ', $text), you might get empty array elements from multiple spaces. Use array_filter() to remove empty elements.

Handling Punctuation: Words might have punctuation attached ("hello!" or "world,"). Use trim($word, '.,!?;:') to clean punctuation from individual words.

Counting Characters Without Spaces: Use str_replace(' ', '', $text) to remove all spaces, then apply strlen() to the result.

Empty Input Handling: Always check if the trimmed input is empty before trying to analyze it. Provide a helpful message like "Please enter some text to analyze."

Extension Challenges

Once you have the basic requirements working, try these optional enhancements:

Submission Requirements

Create a single PHP file (like text_analyzer.php) that handles both form display and text processing. The file should:

Test your analyzer with different types of text: short paragraphs, long articles, poetry, technical writing, and casual conversation. Ensure it produces reasonable results across various content types.

Assessment Criteria

Your project will be evaluated on:

This project challenges you to combine multiple string functions creatively while solving a genuine problem. Content creators, students, and writers regularly need text analysis tools. Your implementation could actually be useful beyond this learning exercise.

Focus on building something that works reliably rather than adding unnecessary complexity. The goal is demonstrating mastery of string manipulation while creating a practical tool that provides genuine value to users.

Remember that text analysis involves both technical string processing and understanding how humans write. Your analyzer should reflect real writing patterns and provide insights that help users improve their communication effectiveness.

← Previous Lesson: Mad Libs Generator