PHP code example of wp-php-toolkit / encoding
1. Go to this page and download the library: Download wp-php-toolkit/encoding library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wp-php-toolkit / encoding example snippets
function WordPress\Encoding\wp_is_valid_utf8;
$samples = array(
'ASCII' => 'just a test',
'UTF-8 pencil' => "\xE2\x9C\x8F",
'latin-1 byte' => "B\xFCch",
'overlong slash' => "\xC1\xBF",
'surrogate half' => "\xED\xB0\x80",
);
foreach ( $samples as $label => $bytes ) {
echo sprintf( "%-14s %s\n", $label . ':', wp_is_valid_utf8( $bytes ) ? 'valid' : 'invalid' );
}
function WordPress\Encoding\wp_scrub_utf8;
$broken = "the byte \xC0 should not be here.";
echo wp_scrub_utf8( $broken ) . "\n";
echo wp_scrub_utf8( ".\xE2\x8C\xE2\x8C." ) . "\n";
function WordPress\Encoding\wp_has_noncharacters;
$samples = array(
'normal text' => 'normal text',
'U+FFFE' => "oops \u{FFFE}",
'U+FDD0' => "hi \u{FDD0} bye",
);
foreach ( $samples as $label => $text ) {
echo sprintf( "%-12s %s\n", $label . ':', wp_has_noncharacters( $text ) ? 'reject' : 'ok' );
}
function WordPress\Encoding\wp_is_valid_utf8;
use function WordPress\Encoding\wp_scrub_utf8;
use function WordPress\Encoding\wp_has_noncharacters;
$inputs = array(
'good' => 'Café',
'latin1' => "caf\xE9",
'overlong' => "x\xC1\xBFy",
'noncharac' => "hi \u{FFFE} there",
);
foreach ( $inputs as $label => $bytes ) {
$valid = wp_is_valid_utf8( $bytes );
$cleaned = wp_scrub_utf8( $bytes );
$weird = wp_has_noncharacters( $cleaned );
echo sprintf( "%-10s valid=%s noncharacter=%s -> %s\n", $label, $valid ? 'Y' : 'N', $weird ? 'Y' : 'N', $cleaned );
}
function WordPress\Encoding\wp_is_valid_utf8;
use function WordPress\Encoding\wp_scrub_utf8;
$rows = array(
1 => 'Plain ASCII',
2 => 'Café',
3 => "caf\xE9",
4 => "weird \xC0 byte",
);
foreach ( $rows as $id => $value ) {
if ( wp_is_valid_utf8( $value ) ) {
echo "#$id ok: $value\n";
continue;
}
$converted = @iconv( 'ISO-8859-1', 'UTF-8', $value );
if ( false !== $converted && wp_is_valid_utf8( $converted ) ) {
echo "#$id recovered as latin1: $converted\n";
} else {
echo "#$id unrecoverable, scrubbing: " . wp_scrub_utf8( $value ) . "\n";
}
}