String Conversion Utility
The text conversions UTF-8 code keeps rewriting, in one place: HTML entities, accents, emoji, MIME encoded words and ASCII-only text.
Why
Each of these is a small function that every project writes again, usually with a bug for a
character nobody tested: accents in a slug, an email subject with "ç", user text with :) or
emoji that a legacy system cannot store.
Convert has them, both ways:
FromUTF8::removeAccent('João'); // "Joao"
FromUTF8::toMimeEncodedWord('João'); // "=?UTF-8?Q?Jo=C3=A3o?=", for email headers
ToUTF8::fromEmoji('Hello :) <3'); // "Hello 😊 ❤️"
FromUTF8::removeEmoji($userInput);
Features
- Convert HTML entities to UTF8 and vice-versa
- Convert ASCII emoticons to emoji characters
- Handle combining characters
- Remove accents from UTF8 text
- Convert text to MIME encoded words (RFC 2047)
- Remove emoji characters
- Convert to ASCII-only text
Documentation
- Converting to UTF8 - Convert from HTML entities, emoticons, and combining characters
- Converting from UTF8 - Convert to HTML entities, remove accents and emoji, ASCII conversion
- Installation - How to install and set up the library
- Examples - Real-world usage examples and common patterns
Quick Start
<?php
// Convert HTML entities to UTF8
$str = \ByJG\Convert\ToUTF8::fromHtmlEntities('João');
echo $str; // João
// Convert UTF8 to HTML entities
$str2 = \ByJG\Convert\FromUTF8::toHtmlEntities('João');
echo $str2; // João
// Remove accents
$str3 = \ByJG\Convert\FromUTF8::removeAccent('João');
echo $str3; // Joao
// Convert to MIME encoded word (for email headers)
$str4 = \ByJG\Convert\FromUTF8::toMimeEncodedWord('João');
echo $str4; // =?UTF-8?Q?Jo=C3=A3o?=
// Convert to ASCII only
$str5 = \ByJG\Convert\FromUTF8::onlyAscii('João');
echo $str5; // Joao
// Convert ASCII emoticons to emoji
$str6 = \ByJG\Convert\ToUTF8::fromEmoji('Hello :) How are you? :D');
echo $str6; // Hello 😊 How are you? 😃
// Remove emoji characters
$str7 = \ByJG\Convert\FromUTF8::removeEmoji('Hello 👋 World 🌍');
echo $str7; // Hello World
For more examples and detailed documentation, see the Examples page.
Installation
Just type:
composer require "byjg/convert"
Running Tests
vendor/bin/phpunit