Current Location: Home> Latest Articles> PHP Simplified and Traditional Chinese Conversion Class Implementation and Tutorial

PHP Simplified and Traditional Chinese Conversion Class Implementation and Tutorial

gitbox 2025-06-30

Introduction

PHP is a widely-used programming language in web development, and as Chinese becomes more commonly used on the internet, the need for Simplified and Traditional Chinese conversion has become prevalent. Therefore, developing a PHP class for Simplified and Traditional Chinese conversion is both meaningful and practical.

Traditional and Simplified Conversion

Traditional to Simplified

Traditional to Simplified conversion is a common requirement in Chinese text processing. Here's a simple example using OpenCC for this conversion:


$opencc = new OpenCC(OpenCC::TW_TO_CN);
$output = $opencc->convert("開放中文轉換");
echo $output;

The `OpenCC::TW_TO_CN` in the code indicates the conversion from Traditional Chinese (TW) to Simplified Chinese (CN).

Simplified to Traditional

Simplified to Traditional conversion is equally common. Below is an example of using OpenCC to convert Simplified Chinese to Traditional Chinese:


$opencc = new OpenCC(OpenCC::CN_TO_TW);
$output = $opencc->convert("开放中文转换");
echo $output;

The `OpenCC::CN_TO_TW` in the code indicates the conversion from Simplified Chinese (CN) to Traditional Chinese (TW).

Complete Traditional and Simplified Conversion Class

To streamline the process, we can integrate both Traditional to Simplified and Simplified to Traditional conversions into one class. Here’s a full PHP class example:


class ChineseConvert {
    private $opencc_tw_to_cn;
    private $opencc_cn_to_tw;

    public function __construct() {
        $this->opencc_tw_to_cn = new OpenCC(OpenCC::TW_TO_CN);
        $this->opencc_cn_to_tw = new OpenCC(OpenCC::CN_TO_TW);
    }

    public function tw_to_cn($string) {
        return $this->opencc_tw_to_cn->convert($string);
    }

    public function cn_to_tw($string) {
        return $this->opencc_cn_to_tw->convert($string);
    }
}

This class initializes two `OpenCC` objects in the constructor—one for Traditional to Simplified conversion and one for Simplified to Traditional conversion. The methods `tw_to_cn` and `cn_to_tw` are used to handle the respective conversions.

Usage Example

Using the `ChineseConvert` class is very straightforward. Here’s an example of how to use it:


$convert = new ChineseConvert();
$tw_string = "開放中文轉換";
$cn_string = $convert->tw_to_cn($tw_string);
echo $cn_string; // Outputs "开放中文转换"

This code demonstrates how to convert the Traditional string “開放中文轉換” to the Simplified string “开放中文转换.” Similarly, converting Simplified to Traditional is just as easy:


$convert = new ChineseConvert();
$cn_string = "开放中文转换";
$tw_string = $convert->cn_to_tw($cn_string);
echo $tw_string; // Outputs "開放中文轉換"

Conclusion

This article introduced how to implement a PHP class for Traditional and Simplified Chinese conversion using OpenCC. The examples demonstrated both Traditional to Simplified and Simplified to Traditional conversion methods. This class can be further extended for more flexible Chinese text conversion based on specific requirements.