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:
- Apply multiple string functions in combination to solve complex problems
- Process and clean user text input effectively
- Perform mathematical calculations on text data
- Parse text structure using string manipulation techniques
- Present analytical data in organized, readable formats
- Handle edge cases in text processing (empty input, special characters, etc.)
Requirements
Your text analyzer must include these specific features:
Basic Counting (Required)
- Total character count (including spaces)
- Total character count (excluding spaces)
- Total word count
- Total sentence count (count periods, exclamation marks, question marks)
Simple Analysis (Required)
- Find the longest word in the text
- Count how many times each word appears (show top 3 most frequent)
- Calculate average word length
String Function Requirements
- Use
trim()
to clean user input - Use
strlen()
for character counting - Use
explode()
to break text into words and sentences - Use
strtolower()
for consistent word comparison - Use at least 4 different string functions total
Input Processing
- Accept text input via HTML textarea
- Clean whitespace from input before processing
- Show helpful error message for empty input
- Handle basic text formatting (extra spaces, line breaks)
Output Format
- Display results as a simple, numbered list
- Show clear labels for each statistic
- Present information in logical order (basic counts first, then analysis)
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:
- Filter Common Words: Exclude words like "the", "and", "a", "is", "to" from your frequency analysis
- Sentence Length Analysis: Calculate the average number of words per sentence
- Word Length Distribution: Show how many short words (1-3 letters), medium words (4-6 letters), and long words (7+ letters) appear in the text
- Punctuation Analysis: Count how many times different punctuation marks appear
- Multiple Text Comparison: Allow users to analyze and compare two different text samples
Submission Requirements
Create a single PHP file (like text_analyzer.php
) that handles both form display and text processing. The file should:
- Present a clean, professional interface for text input
- Process submitted text and generate comprehensive analysis
- Display results in an organized, easy-to-read format
- Handle various edge cases gracefully
- Include proper code organization and comments
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:
- Analysis Accuracy: Do the calculations produce correct and meaningful results?
- String Function Usage: Are string functions applied effectively and appropriately?
- Code Quality: Is the implementation clean, organized, and well-commented?
- User Experience: Is the interface intuitive and the output clear?
- Edge Case Handling: Does the application handle unusual input gracefully?
- Requirements Completeness: Are all specified features implemented and working?
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