2016-04-06 13 views
0

私はワードプレスのcsvからデータベースへのデータの取り込みに取り組んでいます。下は私の関数です。問題は特殊文字(たとえばÂ)を正しく取り出すことができないことです。特別なcharectersでフェッチする。特殊文字を含むデータベースへのcsv

public function process_feed(){ 

    // title, image URL, retail price, sale price, product link, SKU, group ID (new), inventory 

    // CSV Fields 
    // [id] => 00412504024374 
    // [title] => D.L. & Co Votive Candle Gift Set-Multi 
    // [description] => A collection of six 1 oz. votive candles in three scents: two Thorn Apple, two Angels Trumpet and two Lady Rhubarb candles. Each housed in signature hand-blown frosted glass vases. Delightfully packaged in signature silk hat box. 
    // [google_product_category] => Home & Garden > Decor > Candles 
    // [product_type] => Home > Decor & Accessories > Home Fragrance > Candles 
    // [link] => http://www.barneyswarehouse.com/D.L.-&-Co-Votive-Candle-Gift-Set-125046857.html 
    // [image_link] => http://s7d9.scene7.com/is/image/Barneys/125046857 
    // [additional_image_link] => 
    // [condition] => new 
    // [availability] => in stock 
    // [price] => 125.00 USD 
    // [sale_price] => 45.00 USD 
    // [sale_price_effective_date] => 2014-11-17T14:39:06/2015-11-17T14:39:06 
    // [brand] => D.L. & Co 
    // [gtin] => 
    // [mpn] => 00412504024374 
    // [item_group_id] => 125046857 
    // [color] => Multi 
    // [material] => 
    // [pattern] => 
    // [size] => 1 Size 
    // [gender] => Unisex 
    // [age_group] => Adult 
    // [adwords_labels] => 
    // [custom_label_0] => 
    // [custom_label_1] => 
    // [custom_label_2] => 
    // [custom_label_3] => 
    // [custom_label_4] => 

    $file_paths = array(
     get_option('sjr_product_import_file_path_1'), 
     get_option('sjr_product_import_file_path_2') 
    ); 

    if(!empty($file_paths)){ 

     // Capture all product IDs in the feeds to compare against existing products in DB 
     // to deactivate products that are no longer in the feeds. 
     $current_products = array(); 

     foreach($file_paths as $file_path){ 

      $csv = new parseCSV(); 
      $csv->delimiter = "\t"; 
      $csv->enclosure = ""; 
      $csv->sort_by = "item_group_id"; 
      $csv->parse($file_path); 

      if($csv->error === 0){ 

       $current_group = null; 
       $i = $i_all = 1; 

       foreach($csv->data as $product_row){ 

        if($current_group === $product_row['item_group_id']){ 
         $i_all++; 
         continue; 
        } 

        // Only import one of the item group products 
        $current_group = $product_row['item_group_id']; 

        $i++; 

        $current_products[] = $product_row['id']; 

        $args = array(
         'meta_query' => array(
          array(
           'key' => $this->meta_field_prefix . 'product_id', 
           'value' => $product_row['id'] 
          ) 
         ), 
         'post_type' => 'imported_product', 
         'posts_per_page' => 1 
        ); 

        $existing_post = new \WP_Query($args); 

        $post_data = array(
         'post_title'  => $product_row['item_group_id'] . ' - ' . format_product_name($product_row['title']), 
         'post_content' => $product_row['description'], 
         'post_excerpt' => $product_row['description'], 
         'post_status' => 'publish', 
         'post_type'  => 'imported_product', 
         'post_author' => 1, 
         'ping_status' => 'closed', 
         'comment_status' => 'closed', 
        ); 

        // No need for those items 
        $product_row['product_id'] = $product_row['id']; 
        unset($product_row['id'], $product_row['description']); 

        if(!empty($existing_post->posts[0])){ 
         $post_id = $existing_post->posts[0]->ID; 
         $post_data['ID'] = $post_id; 
         wp_update_post($post_data); 
        } else { 
         $post_id = wp_insert_post($post_data); 
        } 

        // The rest are meta fields, add a prefix 
        if (isset($post_id)){ 
         $meta_fields = array(
          'item_group_id' => $product_row['item_group_id'], 
          'product_name' => str_replace(strrchr($product_row['title'], '-'), '', $product_row['title']), 
          'product_id' => $product_row['product_id'], 
          'is_inactive' => 0, 
          'additional_information' => array(
           'image_link' => $product_row['image_link'], 
           'price' => $product_row['price'], 
           'sale_price' => $product_row['sale_price'], 
           'sale_price_effective_date' => $product_row['sale_price_effective_date'], 
           'link' => $product_row['link'], 
           'brand' => $product_row['brand'] 
          ), 
         ); 
         foreach($meta_fields as $key => $value){ 
          if (is_array($value)){ 
           $value = serialize($value); 
          } 
          update_post_meta($post_id, $this->meta_field_prefix . $key, $value); 
         } 
        } 

       } // end foreach 

      } else { // else if($csv->error === 0) 

      } // end if($csv->error === 0) 

     } // end foreach($file_paths as $file_path) 
} 
} 

function format_product_name($title){ 
return str_replace(strrchr($title, '-'), '', $title); 
}; 

DB:

function install(){ 

global $wpdb; 

$charset_collate = $wpdb->get_charset_collate(); 
$db_table = $wpdb->prefix . 'products'; 

$sql = "CREATE TABLE $db_table (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
      `row_id` varchar(22) DEFAULT NULL, 
      `title` varchar(255) DEFAULT '', 
      `description` text, 
      `google_product_category` text, 
      `product_type` text, 
      `link` text, 
      `image_link` text, 
      `additional_image_link` text, 
      `condition` text, 
      `availability` text, 
      `price` text, 
      `sale_price` text, 
      `sale_price_effective_date` text, 
      `brand` text, 
      `gtin` text, 
      `mpn` varchar(22) DEFAULT NULL, 
      `item_group_id` int(22) DEFAULT NULL, 
      `color` text, 
      `material` text, 
      `pattern` text, 
      `size` text, 
      `gender` text, 
      `age_group` text, 
      `adwords_labels` text, 
      `custom_label_0` text, 
      `custom_label_1` text, 
      `custom_label_2` text, 
      `custom_label_3` text, 
      `custom_label_4` text, 
      `feed_file` varchar(255) DEFAULT NULL, 
      `modified` datetime DEFAULT NULL, 
      PRIMARY KEY (`id`), 
      UNIQUE KEY `row_id` (`row_id`), 
      KEY `title` (`title`), 
      KEY `item_group_id` (`item_group_id`) 
     ) $charset_collate;"; 

require_once ABSPATH . 'wp-admin/includes/upgrade.php'; 

dbDelta($sql); 
} // End install() 
+1

文字エンコーディングの問題です.csvとデータベースフィールドの文字エンコーディングは異なります。 –

+0

これで何ができるのですか – Melvin

+0

PHPExcelライブラリを使用して任意のタイプのCSVを読み込み、チェックしますhttp://php.net/manual/en/function.iconv.php php関数iconvはエンコーディングを処理するのに良いオプションです –

答えて

0

すなわち、はutf8_encode /デコードを使用して機能format_product_nameを使用してみてください:

function format_product_name($title){ 
$title = str_replace("-", "", $title); 
$title = utf8_encode($title); 
//you can also try $title = utf8_decode($title); 
return $title; 
}; 

その後ポストコンテンツをフォーマットするためにそれを使用します。

... 
'post_title'  => $product_row['item_group_id'] . ' - ' . format_product_name($product_row['title']), 
'post_content' => format_product_name($product_row['description']), 
'post_excerpt' => format_product_name($product_row['description']), 
... 

注:

それはUTFだことを確認するために設定し、データベースフィールドエンコーディングを確認し、ない場合は、あなたがそのため、それに合わせて、CSV形式のコンテンツをエンコードする必要があります、iconvまたは類似のPHPの関数を使用します。

+0

str_replace(strrchr($ title、 ' - ')、 ''、$ title);上記のstr_replaceは同じ動作をします。 – Melvin

+0

'' ''(何もない)のためにすべての ' - 'を置きたいと思っていますか? –

+0

'-'の代わりに' space'が必要な場合は、 '$ title = str_replace(" - "、" $ title); 'を使用します。私は今に行って、それがうまくいけばいい! –

関連する問題