Sunday, February 9, 2025

PHP-Humanizer: Make Your PHP Output More Human-Friendly


We often deal with raw data that isn't always user-friendly. Numbers, strings, and dates can look technical and intimidating. The php-humanizer package by Norbert Orzechowicz is here to change that, offering a simple yet powerful way to present data in a more readable and human-friendly format.

While Laravel provides some built-in humanization features (like ordinal numbers, string helpers, and Carbon for dates), php-humanizer shines with its broader range of functionalities, especially useful in standalone PHP projects or any PHP application beyond Laravel.

What Can php-humanizer Do?

This package offers a variety of helpful methods, including:

  • String Humanization: Transform technical strings into readable phrases. For example, StringHumanizer::humanize('field_name') outputs "Field Name," instantly making database field names more presentable.

  • Number Formatting:

    • Binary Suffixes: Easily display file sizes with binary suffixes (kB, MB, GB). NumberHumanizer::binarySuffix(1048576 * 5) elegantly displays "5 MB."
    • Metric Suffixes: Shorten large numbers using metric suffixes (k, M, B). NumberHumanizer::metricSuffix(3500000) becomes "3.5M."
  • Oxford Lists: Format lists in a grammatically correct and natural way. CollectionHumanizer::oxford(['Michal', 'Norbert', 'Lukasz']) outputs "Michal, Norbert, and Lukasz," handling various list lengths and conjunctions.

Key Features at a Glance:

  • Number Formatting: Ordinal values, metric suffixes, Roman numerals.
  • Text Manipulation: Case conversion (camelCase, snake_case), smart text truncation, line breaks.
  • Time/Date Humanizing: Relative time (e.g., "2 hours ago"), natural date differences (e.g., "1 month").
  • File Size Formatting: Human-readable byte sizes (KB, MB, GB) with configurable precision.
  • Oxford List Formatting: Natural language lists with different joining styles and locale support.
  • Collection Operations: Natural language collection formatting with custom rules.

Why Use php-humanizer?

  • Improved User Experience: Present data in a way that's easy for users to understand.
  • Enhanced Readability: Make your application output more engaging and less technical.
  • Increased Efficiency: Save time and effort by using pre-built humanization functions.

Getting Started

Ready to make your PHP output more human? Check out the project's README on GitHub for detailed installation instructions, usage examples, and a complete list of supported languages. You'll find everything you need to get started with php-humanizer and transform your data into a more user-friendly experience.

Example

use Coduo\PHPHumanizer\StringHumanizer;

StringHumanizer::humanize('field_name'); // "Field Name"
StringHumanizer::humanize('user_id'); // "User"
StringHumanizer::humanize('field_name', false); // "field name"

Learn More:

Explore the full capabilities of php-humanizer on GitHub: https://github.com/coduo/php-humanizer

0 comments:

Post a Comment