Skip to content

Instantly share code, notes, and snippets.

@Zegnat Zegnat/pickAnyEncoding.php
Last active May 22, 2017

Embed
What would you like to do?
<style>
table {
border-collapse: collapse;
}
th, td {
padding: .5em 2em .5em .5em;
border-right: 1px dotted #AAA;
border-bottom: 1px solid #CCC;
}
th {
text-align: left;
border-bottom: 2px solid #000;
}
.name td:first-child + td,
.name td:first-child + td + td {
background-color: lightgreen;
}
</style>
<table>
<thead>
<tr>
<th>Encoding</th>
<th><pre><code>mb_substr($input)</code></pre></th>
<th><pre><code>json_encode(
mb_substr($input)
)</code></pre></th>
<th><pre><code>mb_substr(
mb_convert_encoding($input)
)</code></pre></th>
<th><pre><code>json_encode(
mb_substr(
mb_convert_encoding($input)
)
)</code></pre></th>
</tr>
</thead>
<tbody><?php
//var_dump(mb_list_encodings());die();
$input = 'Snack 👩🏻‍🎤 with Asha';
$offsets = json_decode('[{"indices":[19,23],"id":"80541223","type":"user"}]', true);
foreach ($offsets as $offset) {
foreach (mb_list_encodings() as $encoding) {
$name = mb_substr($input, $offset['indices'][0], $offset['indices'][1], $encoding);
$converted = mb_convert_encoding($input, $encoding, 'UTF-8');
$convertedName = mb_substr($converted, $offset['indices'][0], $offset['indices'][1], $encoding);
$row = '<tr class="' . ($name === 'Asha' ? 'name' : '') . '"><td>';
$row .= $encoding;
$row .= '</td><td>';
$row .= $name;
$row .= '</td><td>';
$row .= json_encode($name);
$row .= '</td><td>';
$row .= $convertedName;
$row .= '</td><td>';
$row .= json_encode($convertedName);
$row .= '</td></tr>';
echo $row;
}
}
?>
</tbody>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.