Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CB9TOIIIA/3e1080d2b56d54444c4c1f96eb230b45 to your computer and use it in GitHub Desktop.
Save CB9TOIIIA/3e1080d2b56d54444c4c1f96eb230b45 to your computer and use it in GitHub Desktop.
Вырезаем мусорные специальные символы на php Чулан* Здравствуйте, хотел бы поделится способом вырезания специальных символов на php. Столкнулся я с данной проблемой при написании поисковика по базе youtube. В результатах api в именах и описаниях видео попадались символы, сердечки, стрелки, наушники и т.д (�►◄). Готовых решений я не нашел, пришло…
/**
* Функция была взята с php.net
**/
function utf8_str_split($str) {
// place each character of the string into and array
$split=1;
$array = array();
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( $array, $key );
}
return $array;
}
/**
* Функция вырезки
* @param <string> $str
* @return <string>
*/
function clearstr($str){
$sru = 'ёйцукенгшщзхъфывапролджэячсмитьбю';
$s1 = array_merge(utf8_str_split($sru), utf8_str_split(strtoupper($sru)), range('A', 'Z'), range('a','z'), range('0', '9'), array('&',' ','#',';','%','?',':','(',')','-','_','=','+','[',']',',','.','/','\\'));
$codes = array();
for ($i=0; $i<count($s1); $i++){
$codes[] = ord($s1[$i]);
}
$str_s = utf8_str_split($str);
for ($i=0; $i<count($str_s); $i++){
if (!in_array(ord($str_s[$i]), $codes)){
$str = str_replace($str_s[$i], '', $str);
}
}
return $str;
}
?>
echo clearstr('te�s►t◄');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment