Skip to content

Instantly share code, notes, and snippets.

@AlekVolsk
Last active January 10, 2021 18:29
Show Gist options
  • Save AlekVolsk/1d364cb6b8f0ab39e010093925a6b0af to your computer and use it in GitHub Desktop.
Save AlekVolsk/1d364cb6b8f0ab39e010093925a6b0af to your computer and use it in GitHub Desktop.
Вырезать из строки иконочные символы
<?php
function icontrim($str)
{
$out = '';
$split = 1;
$arr = [];
$excludes = [
8211, // – shord dash
8212, // — long dash
8216, // ‘ quote
8217, // ’ quote
8220, // “ quote
8221, // ” quote
8222, // „ quote
8230, // … ellipsis
8242, // ′ apostroph
8265, // ⁉ exclamation with a question
8364, // € euro
8381, // ₽ rouble
8482, // ™ trademark
8722, // − short dash (alternate)
8776, // ≈ relatively
];
for ($i = 0; $i < strlen($str);) {
$value = ord($str[$i]);
if ($value > 127) {
if ($value >= 192 && $value <= 223) {
$split = 2;
} elseif ($value >= 224 && $value <= 239) {
$split = 3;
} elseif ($value >= 240 && $value <= 247) {
$split = 4;
}
} else {
$split = 1;
}
$key = null;
for ($j = 0; $j < $split; $j++, $i++) {
$key .= $str[$i];
}
array_push($arr, $key);
}
foreach ($arr as $char) {
$code = mb_ord($char);
if (
!(
_intBetween($code, 8205, 11917) ||
_intBetween($code, 12295, 12336) ||
_intBetween($code, 126976, 129685) ||
$code >= 917600
)
|| in_array($code, $excludes)
) {
$out .= $char;
}
}
return trim(preg_replace('~\p{C}+~u', '', $out));
}
function _intBetween($int, $min, $max)
{
return ($int >= $min && $int <= $max);
}
// Пример работы
$s = '🔥🔥🔥 Суперскидки здесь! ❤️👉';
echo '<pre>';
var_dump($s);
// string(58) "🔥🔥🔥 Суперскидки здесь! ❤️👉"
var_dump(icontrim($s));
// string(34) "Суперскидки здесь!"
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment