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; // HumanizeStringHumanizer::humanize('field_name'); // "Field Name"StringHumanizer::humanize('user_id'); // "User"StringHumanizer::humanize('field_name', false); // "field name" // Binary suffixNumberHumanizer::binarySuffix(1024); // "1 kB"NumberHumanizer::binarySuffix(1536); // "1.5 kB"NumberHumanizer::binarySuffix(1048576 * 5); // "5 MB"NumberHumanizer::binarySuffix(1073741824 * 2); // "2 GB" // Metric suffixNumberHumanizer::metricSuffix(101); // "101"NumberHumanizer::metricSuffix(1000); // "1k"NumberHumanizer::metricSuffix(1240); // "1.2k"NumberHumanizer::metricSuffix(1240000); // "1.24M"NumberHumanizer::metricSuffix(3500000); // "3.5M" // OxfordCollectionHumanizer::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.