Charset Konvertieren

Java unterstützt auf einfache Art und Weise die Konvertierung von Zeichensätzen:

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

Charset utf8 = Charset.forName("UTF-8");
Charset iso88591 = Charset.forName("ISO-8859-1");

//get byte array from string
ByteBuffer utf8Buffer = ByteBuffer.wrap( utf8String.getBytes() );

//convert byte array from UTF-8 to ISO-8859-1
ByteBuffer iso88591Buffer = iso88591.encode( utf8.decode( utf8Buffer ) );

//parse byte array to ISO-8859-1 string
String iso88591String = new String( iso88591Buffer.array(), "ISO-8859-1" ); 

Nähere Angaben zu den unterstützten Zeichensätzen findet sich in der Dokumentation der Klasse Charset.

Um mit Java die Daten aus einer Oracle Datenbank von ISO-8859-1 in UTF-8 konvertieren kann man folgendermassen vorgehen:

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@server:port:sid", "username", "password");

PreparedStatement selectStmt = connection.prepareStatement("SELECT id,subject FROM messages");
ResultSet resultSet = selectStmt.executeQuery();

Charset utf8 = Charset.forName("UTF-8");
Charset iso88591 = Charset.forName("ISO-8859-1");

System.out.println( "Converting Records..." );

while( resultSet.next() ){
    byte[] raw = resultSet.getBytes("subject");
    ByteBuffer iso88591Buffer = ByteBuffer.wrap(raw);

    //do the actual charset conversion
    ByteBuffer utf8Buffer = utf8.encode( iso88591.decode(iso88591Buffer) );
    
    try{
        String iso88591Str = new String(iso88591Buffer.array(),"UTF8");
        String utf8Str = new String(utf8Buffer.array(),"UTF8");
        
        System.out.println("UTF8: '" + iso88591Str + "' => '" + utf8Str + "'");
        
        PreparedStatement updateStmt = connection.prepareStatement("UPDATE messages SET subject = ? WHERE id = ?");
        updateStmt.setString(1,utf8Str);
        updateStmt.setInt(2, resultSet.getInt("id"));
        updateStmt.executeUpdate();
        updateStmt.close();
        
    } catch( UnsupportedEncodingException e ){}
}

if( selectStmt != null ){
    selectStmt.close();
}

WooCommerce: E-Mail Adresse bestätigen

Im WooCommerce Checkout Prozess lässt sich ganz einfach ein zusätzliches Feld für die Validierung der eingegebenen E-Mail-Adresse hinzufügen:

billing_email_confirm

add_filter('woocommerce_checkout_fields',function($fields){
    $billing_email_confirm = array(
        'label'            => 'E-Mail-Adresse bestätigen',
        'placeholder'    => '',
        'required'        => true,
        'class'            => apply_filters('woocommerce_billing_email_confirm_field_class',array('form-row-first')),
        'clear'            => true,
        'validate'        => array('email'),
    );
    $billing_email_index = array_search('billing_email', array_keys($fields['billing']));
  
    if( $billing_email_index ){
        $fields['billing'] = array_slice($fields['billing'],0,$billing_email_index + 1,true) +
            array( 'billing_email_confirm' => $billing_email_confirm ) +
            array_slice($fields['billing'],$billing_email_index + 1,null,true);
    } else {
        $fields['billing']['billing_email_confirm'] = $billing_email_confirm;
    }

    return $fields;
});

add_filter('woocommerce_process_checkout_field_billing_email_confirm',function($email_confirm=''){
    global $woocommerce;
    $billing_email = $woocommerce->checkout->posted['billing_email'];

    if( strtolower($email_confirm) != strtolower($billing_email) ){
        $notice = '<strong>E-Mail-Adressen stimmen nicht überein</strong>';

        if ( version_compare( WC_VERSION, '2.3', '<' ) ) {
            $woocommerce->add_error($notice);
        } else {
            wc_add_notice($notice,'error');
        }
    }

    return $email_confirm;
});

add_filter('default_checkout_billing_email_confirm',function($value=null,$field='billing_email_confirm'){
    if( is_user_logged_in() ){
        global $current_user;
        $value = $current_user->user_email;
    }
    return $value;
},10,2);