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

Discussion in 'PHP' started by rcc0023, 28 Sep 2009.

  1. rcc0023

    rcc0023 Elder - Старейшина

    Joined:
    3 Jul 2008
    Messages:
    100
    Likes Received:
    6
    Reputations:
    0
    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] ); удаляет пол контента?
     
  2. Kaimi

    Kaimi Well-Known Member

    Joined:
    23 Aug 2007
    Messages:
    1,732
    Likes Received:
    811
    Reputations:
    231
    Используй
    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;
            }
     
    _________________________
    1 person likes this.
  3. rcc0023

    rcc0023 Elder - Старейшина

    Joined:
    3 Jul 2008
    Messages:
    100
    Likes Received:
    6
    Reputations:
    0
    исказило часть символов..
     
  4. rcc0023

    rcc0023 Elder - Старейшина

    Joined:
    3 Jul 2008
    Messages:
    100
    Likes Received:
    6
    Reputations:
    0
    еще актуально!
     
  5. krypt3r

    krypt3r Elder - Старейшина

    Joined:
    27 Apr 2007
    Messages:
    1,507
    Likes Received:
    389
    Reputations:
    101
    Забейте на iconv и попробуйте
    PHP:
    string mb_convert_encoding  string $str  string $to_encoding  [, mixed $from_encoding  ] )
     
    1 person likes this.
  6. WNZRS

    WNZRS Member

    Joined:
    3 Sep 2009
    Messages:
    294
    Likes Received:
    52
    Reputations:
    1
    iconv('utf-8', 'cp1251//IGNORE', $result);
    если это исправит, значит обрезалось из-за символа,которого нет в cp1251, я с таким столкнулся когда парсил википедию
     
    1 person likes this.