PHP code example of sleep-cat / opencc-php

1. Go to this page and download the library: Download sleep-cat/opencc-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sleep-cat / opencc-php example snippets


    [
        'providers' => [
            // ...
            AlphaSnow\OpenCC\ServiceProvider::class,
        ],
        'aliases' => [
            // ...
            'OpenCC' => AlphaSnow\OpenCC\Facade::class,
        ]
    ];
    

    return [
        // 执行文件的路径,默认:/usr/bin/opencc
        'binary_path'=> env('OPENCC_BINARY', '/usr/bin/opencc'),
        // 预设配置文件夹的路径,默认:/usr/share/opencc
        'config_path'=> env('OPENCC_CONFIG', '/usr/share/opencc'),
    ];
    

// laravel应用可用外观
// 参数1:待转换内容, 参数2:预设配置(可省略.json)
$simplified = \OpenCC::convert('天氣乍涼人寂寞,光陰須得酒消磨。且來花裏聽笙歌。','t2s.json');
print_r($simplified);
// 天气乍凉人寂寞,光阴须得酒消磨。且来花里听笙歌。

$traditional = \OpenCC::convert('四面垂杨十里荷。问云何处最花多。画楼南畔夕阳和。', 's2t');
print_r($traditional);
// 四面垂楊十里荷。問云何處最花多。畫樓南畔夕陽和。

use AlphaSnow\OpenCC\Command;
use AlphaSnow\OpenCC\OpenCC;

$command = new Command('/usr/bin/opencc','/usr/share/opencc');
$openCC = new OpenCC($command);

$result = $openCC->convert('天氣乍涼人寂寞,光陰須得酒消磨。且來花裏聽笙歌。','t2s.json');
print_r($result);
// 天气乍凉人寂寞,光阴须得酒消磨。且来花里听笙歌。
bash
    composer 
bash
    php artisan vendor:publish --provider="AlphaSnow\OpenCC\ServiceProvider"