Форум АНТИЧАТ

Форум АНТИЧАТ (https://forum.antichat.xyz/index.php)
-   PHP, PERL, MySQL, JavaScript (https://forum.antichat.xyz/forumdisplay.php?f=37)
-   -   php, iconv, utf-8 - обрезает контент (https://forum.antichat.xyz/showthread.php?t=144161)

rcc0023 28.09.2009 11:50

php, iconv, utf-8 - обрезает контент
 
PHP код:

<?php
set_time_limit
(3600);

$url=array( 
    
        
'11723' => 'Брно'
        
'11414' => 'Карловы Вары'
        
'11418' => 'Марианские Лазни'
        
'11448' => 'Пльзень'
        
'11518' => 'Прага'
        
'11790' => 'Черный Дул'
        
'11412' => 'Яхимов' 
);

foreach( 
$url as $k=>$v ){
echo 
'тип: '.$url[$k].'<br>';
    
$str=file_get_contents'http://weather.yandex.ru/' $k '/details/' );
    
preg_match"'(<table class=\"b-forecast-details\">.+<\/table>)'is"$str$ar );
    if(
$k==11414){echo iconv'UTF-8''CP1251'$ar[1] );exit;}
    if ( isset( 
$ar[1] ) ) {
    echo 
'загружаем...<br>';
        
$ar[1]=str_replace('http://img.yandex.net/i/weather/wind/','/images/weather/',$ar[1]);
        
$ar[1]=str_replace('http://img.yandex.net/i/weather/moon/','/images/weather/',$ar[1]);
        if(
file_put_contentsdirnamedirname__FILE__ ) ) . '/weather/' $k '.html'iconv'UTF-8''CP1251'$ar[1] ) ))echo 'сохраняем...<br>';
    }
}
?>

Почему на странице http://weather.yandex.ru/11414/details/ функция iconv( 'UTF-8', 'CP1251', $ar[1] ); удаляет пол контента?

Kaimi 28.09.2009 12:07

Используй
PHP код:

function utf8_to_cp1251($s)
        {
            for (
$c=0;$c<strlen($s);$c++)
            {
               
$i=ord($s[$c]);
               if (
$i<=127$out.=$s[$c];
                   if (
$byte2){
                       
$new_c2=($c1&3)*64+($i&63);
                       
$new_c1=($c1>>2)&5;
                       
$new_i=$new_c1*256+$new_c2;
                   if (
$new_i==1025){
                       
$out_i=168;
                   } else {
                       if (
$new_i==1105){
                           
$out_i=184;
                       } else {
                           
$out_i=$new_i-848;
                       }
                   }
                   
$out.=chr($out_i);
                   
$byte2=false;
                   }
               if ((
$i>>5)==6) {
                   
$c1=$i;
                   
$byte2=true;
               }
            }
            return 
$out;
        } 


rcc0023 28.09.2009 13:24

исказило часть символов..

rcc0023 30.09.2009 09:49

еще актуально!

krypt3r 30.09.2009 10:37

Забейте на iconv и попробуйте
PHP код:

string mb_convert_encoding  string $str  string $to_encoding  [, mixed $from_encoding  ] ) 


WNZRS 30.09.2009 13:08

iconv('utf-8', 'cp1251//IGNORE', $result);
если это исправит, значит обрезалось из-за символа,которого нет в cp1251, я с таким столкнулся когда парсил википедию


Время: 07:49