A simple UTF-8 sanitizer written in PHP 8 using iconv. This code is based after this StackOverflow response and the W3C UTF-8 verification recommended regular expression.
<?php
$sanitizer = new \CodeInc\Utf8Sanitizer\Utf8Sanitizer();
// validates a UTF-8 string
$sanitizer->isValidUtf8("A valid UTF-8 string."); // true
// sanitizes a string
$validString = $sanitizer->sanitize("An invalid UTF-8 string.");
This library is available through Packagist and can be installed using Composer:
composer require codeinc/utf8-sanitizer
This library is published under the MIT license (see the LICENSE file).