Add whitespace between Chinese and other letters

Interesting question. This could be a useful part of a specific language file. It cannot be done in CSS, because CSS is (mostly) character agnostic. But using a filter and PHP it is possible and on topic:

add_filter( 'the_content', 't5_chinese_spacing' );

function t5_chinese_spacing( $content )
{
    return preg_replace(
        '~([^\p{Han}]*)(\p{Han}+)([^\p{Han}])~imUxu',
        '\1 \2 \3',
        $content
    );
}

Be aware your internal encoding has to be UTF-8 to make that work. While WordPress works with other encodings too, PHP needs UTF-8 for this. Read WordPress Database Charset and Collation Configuration to make sure everything is set up properly.

Also note I am using PHP’s character class for Han unification here. This is not perfect, some punctuation marks Chinese writers might use are not included.

Leave a Comment