substr

Печать RSS
391


Господин ПЖ
0
iconv_substr($str, $offset, $length, 'utf-8');
N

Эцилопп сией тентуры
0
<?php
function sub($str){
return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,100}).*#s','$1',$str);}
?>
Изменил: Nu3oN (24.01.2010 / 19:29)
A

Оранжевые штаны
0
12 и этим лишь зря грузишь сервер
Автор
Чатланин
0
iconv_substr - тоже самое.

Господин ПЖ
0
тогда используй 2 функции
<?
function utf_to_win($str) { 
if (function_exists('mb_convert_encoding')) return mb_convert_encoding($str, 'windows-1251', 'utf-8');
if (function_exists('iconv')) return iconv('utf-8', 'windows-1251', $str);
$utf8win1251 = array( 
"А"=>"\xC0","Б"=>"\xC1","В"=>"\xC2","Г"=>"\xC3","Д"=>"\xC4","Е"=>"\xC5","Ё"=>"\xA8","Ж"=>"\xC6","З"=>"\xC7","И"=>"\xC8","Й"=>"\xC9","К"=>"\xCA","Л"=>"\xCB","М"=>"\xCC",
"Н"=>"\xCD","О"=>"\xCE","П"=>"\xCF","Р"=>"\xD0","С"=>"\xD1","Т"=>"\xD2","У"=>"\xD3","Ф"=>"\xD4","Х"=>"\xD5","Ц"=>"\xD6","Ч"=>"\xD7","Ш"=>"\xD8","Щ"=>"\xD9","Ъ"=>"\xDA",
"Ы"=>"\xDB","Ь"=>"\xDC","Э"=>"\xDD","Ю"=>"\xDE","Я"=>"\xDF","а"=>"\xE0","б"=>"\xE1","в"=>"\xE2","г"=>"\xE3","д"=>"\xE4","е"=>"\xE5","ё"=>"\xB8","ж"=>"\xE6","з"=>"\xE7",
"и"=>"\xE8","й"=>"\xE9","к"=>"\xEA","л"=>"\xEB","м"=>"\xEC","н"=>"\xED","о"=>"\xEE","п"=>"\xEF","р"=>"\xF0","с"=>"\xF1","т"=>"\xF2","у"=>"\xF3","ф"=>"\xF4","х"=>"\xF5",
"ц"=>"\xF6","ч"=>"\xF7","ш"=>"\xF8","щ"=>"\xF9","ъ"=>"\xFA","ы"=>"\xFB","ь"=>"\xFC","э"=>"\xFD","ю"=>"\xFE","я"=>"\xFF"); 
return strtr($str, $utf8win1251); 
}

Господин ПЖ
0
<?
function utf_substr($str, $offset, $length = null){
if (function_exists('mb_substr')) return mb_substr($str, $offset, $length, 'utf-8');
if (function_exists('iconv_substr')) return iconv_substr($str, $offset, $length, 'utf-8');
$str = utf_to_win($str);
$str = substr($str,$offset,$length);
return win_to_utf($str);
}

Господин ПЖ
0
Точнее нужно 3 функции, вот еще одна
<?
function win_to_utf($str) { 
if (function_exists('mb_convert_encoding')) return mb_convert_encoding($str, 'utf-8', 'windows-1251');
if (function_exists('iconv')) return iconv('windows-1251', 'utf-8', $str);
$win1251utf8 = array( 
"\xC0"=>"А","\xC1"=>"Б","\xC2"=>"В","\xC3"=>"Г","\xC4"=>"Д","\xC5"=>"Е","\xA8"=>"Ё","\xC6"=>"Ж","\xC7"=>"З","\xC8"=>"И","\xC9"=>"Й","\xCA"=>"К","\xCB"=>"Л","\xCC"=>"М",
"\xCD"=>"Н","\xCE"=>"О","\xCF"=>"П","\xD0"=>"Р","\xD1"=>"С","\xD2"=>"Т","\xD3"=>"У","\xD4"=>"Ф","\xD5"=>"Х","\xD6"=>"Ц","\xD7"=>"Ч","\xD8"=>"Ш","\xD9"=>"Щ","\xDA"=>"Ъ",
"\xDB"=>"Ы","\xDC"=>"Ь","\xDD"=>"Э","\xDE"=>"Ю","\xDF"=>"Я","\xE0"=>"а","\xE1"=>"б","\xE2"=>"в","\xE3"=>"г","\xE4"=>"д","\xE5"=>"е","\xB8"=>"ё","\xE6"=>"ж","\xE7"=>"з",
"\xE8"=>"и","\xE9"=>"й","\xEA"=>"к","\xEB"=>"л","\xEC"=>"м","\xED"=>"н","\xEE"=>"о","\xEF"=>"п","\xF0"=>"р","\xF1"=>"с","\xF2"=>"т","\xF3"=>"у","\xF4"=>"ф","\xF5"=>"х",
"\xF6"=>"ц","\xF7"=>"ч","\xF8"=>"ш","\xF9"=>"щ","\xFA"=>"ъ","\xFB"=>"ы","\xFC"=>"ь","\xFD"=>"э","\xFE"=>"ю","\xFF"=>"я"); 
return strtr($str, $win1251utf8); 
} 
Изменил: Вантуз-мен (24.01.2010 / 21:06)
Автор
Чатланин
0
Спасибо.

2000 лет д.н.э.
0
Че то я не осилил..
МБстринг уже не кошерно что ли?
Все отлично режет.

Господин ПЖ
0
ramzes (Сегодня / 21:15)
Че то я не осилил..
МБстринг уже не кошерно что ли?
Все отлично режет.
в каждой из 3-х функций идет проверка на стандартные библиотеки такие как iconv или mb_string, если таковы отсутствуют или не установлены идет перекодировка по символам
Стикеры / Теги / Правила / Топ тем / Топ постов / Поиск