News

Humanize String, Number, and Date Values into Readable Formats

Published
Humanize String, Number, and Date Values into Readable Formats  image

The php-humanizer package by Norbert Orzechowicz humanizes readable values such as numbers, strings, dates, and more. In Laravel projects, we have features like number ordinal values, string helper methods, and Carbon for dates, but this package has other useful features if you're working on a standalone PHP project, any PHP application really:

use Coduo\PHPHumanizer\NumberHumanizer;
use Coduo\PHPHumanizer\CollectionHumanizer;
use Coduo\PHPHumanizer\StringHumanizer;
 
// Humanize
StringHumanizer::humanize('field_name'); // "Field Name"
StringHumanizer::humanize('user_id'); // "User"
StringHumanizer::humanize('field_name', false); // "field name"
 
// Binary suffix
NumberHumanizer::binarySuffix(1024); // "1 kB"
NumberHumanizer::binarySuffix(1536); // "1.5 kB"
NumberHumanizer::binarySuffix(1048576 * 5); // "5 MB"
NumberHumanizer::binarySuffix(1073741824 * 2); // "2 GB"
 
// Metric suffix
NumberHumanizer::metricSuffix(101); // "101"
NumberHumanizer::metricSuffix(1000); // "1k"
NumberHumanizer::metricSuffix(1240); // "1.2k"
NumberHumanizer::metricSuffix(1240000); // "1.24M"
NumberHumanizer::metricSuffix(3500000); // "3.5M"
 
// Oxford
CollectionHumanizer::oxford(['Michal', 'Norbert', 'Lukasz', 'Pawel'], 2); // "Michal, Norbert, and 2 others"
CollectionHumanizer::oxford(['Michal', 'Norbert', 'Lukasz'], 2); // "Michal, Norbert, and 1 other"
CollectionHumanizer::oxford(['Michal', 'Norbert']); // "Michal and Norbert"

Main Features

  • Number Formatting
    • Converts numbers to ordinal values (1st, 2nd, 3rd)
    • Formats numbers with metric suffixes (1K, 1M, 1B)
    • Converts numbers to Roman numerals
  • Text Manipulation
    • Converts strings to/from camelCase, snake_case, etc.
    • Truncates text with smart word boundaries
    • Adds proper line breaks for text wrapping
  • Time/Date Humanizing
    • Converts timestamps to relative time (2 hours ago, in 3 days)
    • Provides natural date differences (1 month, 2 years)
    • Supports multiple languages/locales
  • File Size Formatting
    • Converts bytes to human-readable sizes (1KB, 2MB, 3GB)
    • Supports binary and decimal units
    • Configurable precision
  • Oxford List Formatting
    • Converts arrays to natural language lists
    • Supports different joining styles (A, B and C vs A, B, and C)
    • Handles different locales and separators
  • Collection Operations
    • Provides natural language collection formatting
    • Supports custom formatters and rules
    • Handles empty collections gracefully

The project's readme has more examples and lists all the supported languages. You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Building EasyReply: How We Used Laravel to Unify Customer Support image

Building EasyReply: How We Used Laravel to Unify Customer Support

Read article
PostgreSQL Monitoring and Schema Linting for Laravel with Vacuum image

PostgreSQL Monitoring and Schema Linting for Laravel with Vacuum

Read article
PayZephyr: One Payment API for Stripe, Paystack, and PayPal image

PayZephyr: One Payment API for Stripe, Paystack, and PayPal

Read article
Bifrost Turns One With AI Builds and New Workflows image

Bifrost Turns One With AI Builds and New Workflows

Read article
Preview Blade Templates in macOS Finder with Quick Blade image

Preview Blade Templates in macOS Finder with Quick Blade

Read article
Artisan Debugging Commands in Laravel Telescope 5.24.0 image

Artisan Debugging Commands in Laravel Telescope 5.24.0

Read article