Convert number into chinese string with php

  1. convert from int to chinese string e.g. 125 -> 一百二十五
 function numToWord($num){
     $chiNum = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
     $chiUni = array('','十', '百', '千', '萬', '十', '百', '千', '億');

     $chiStr = '';
     $num_str = (string)$num;
     $count = strlen($num_str);
     $last_flag = true; // pervious number is 0?
     $zero_flag = true; // first number?
     $temp_num = null;
 
     if ($count == 2) { // 2 digit number
         $temp_num = $num_str[0];
         $chiStr = $temp_num == 1 ? $chiUni[1] : $chiNum[$temp_num].$chiUni[1];
         $temp_num = $num_str[1];
         $chiStr .= $temp_num == 0 ? '' : $chiNum[$temp_num];
     }else if($count > 2){
         $index = 0;
         for ($i=$count-1; $i >= 0 ; $i--) {
                 $temp_num = $num_str[$i];
                 if ($temp_num == 0) {
                     if (!$zero_flag && !$last_flag ) {
                         $chiStr = $chiNum[$temp_num]. $chiStr;
                         $last_flag = true;
                     }
 
                     if($index == 4 && $temp_num == 0){
                         $chiStr = $chiUni[4].$chiStr;
                     }
                 }else{
                     if($i == 0 && $temp_num == 1 && $index == 1 && $index == 5){
                         $chiStr = $chiUni[$index%9] .$chiStr;
                     }else{
                         $chiStr = $chiNum[$temp_num].$chiUni[$index%9] .$chiStr;
                     }
                     $zero_flag = false;
                     $last_flag = false;
                 }
                 $index ++;
         }
     }else{
         $chiStr = $chiNum[$num_str[0]];
     }
     return $chiStr;
 } 

2. convert from chinese string into int, eg. 一百二十五 > 125

 function cn2num($string){
     if(is_numeric($string)){
         return $string;
     }
     // '仟' => '千','佰' => '百','拾' => '十',
     $string = str_replace('仟', '千', $string);
     $string = str_replace('佰', '百', $string);
     $string = str_replace('拾', '十', $string);
     $num = 0;
     $wan = explode('萬', $string);
     if (count($wan) > 1) {
         $num += cn2num($wan[0]) * 10000;
         $string = $wan[1];
     }
     $qian = explode('千', $string);
     if (count($qian) > 1) {
         $num += cn2num($qian[0]) * 1000;
         $string = $qian[1];
     }
     $bai = explode('百', $string);
     if (count($bai) > 1) {
         $num += cn2num($bai[0]) * 100;
         $string = $bai[1];
     }
     $shi = explode('十', $string);
     if (count($shi) > 1) {
         $num += cn2num($shi[0] ? $shi[0] : '一') * 10;
         $string = $shi[1] ? $shi[1] : '零';
     }
     $ling = explode('零', $string);
     if (count($ling) > 1) {
         $string = $ling[1];
     }
     $d = array(
         '一' => '1','二' => '2','三' => '3','四' => '4','五' => '5','六' => '6','七' => '7','八' => '8','九' => '9',
         '壹' => '1','贰' => '2','叁' => '3','肆' => '4','伍' => '5','陆' => '6','柒' => '7','捌' => '8','玖' => '9',
         '零' => 0, '0' => 0, 'O' => 0, 'o' => 0,
         '两' => 2
     );
     return $num + @$d[$string];
 } 
Posted in Development | Tagged , | Leave a comment

TDE in SQL Server

Implementing TDE in SQL server

-- Create Master Key
USE Master;
GO
CREATE MASTER KEY ENCRYPTION
BY PASSWORD='STRONGPASSWORD';
GO

-- Create Certificate protected by master key
CREATE CERTIFICATE TDE_Cert
WITH 
SUBJECT='Database_Encryption';
GO

-- Create Database Encryption Key
USE <DB>
GO
CREATE DATABASE ENCRYPTION KEY
WITH ALGORITHM = AES_256
ENCRYPTION BY SERVER CERTIFICATE TDE_Cert;
GO

-- Enable Encryption
ALTER DATABASE <DB>
SET ENCRYPTION ON;
GO

-- Backup Certificate
BACKUP CERTIFICATE TDE_Cert
TO FILE = 'C:\backup\TDE_Cert'
WITH PRIVATE KEY (file='C:\backup\TDE_CertKey.pvk',
ENCRYPTION BY PASSWORD='STRONGPASSWORD') 

You must remember where you backed up the certificate and the encryption/decryption password.

Restoring a Certificate

-- Create Master key on the secondary server
USE Master;
GO
CREATE MASTER KEY ENCRYPTION
BY PASSWORD='STRONGPASSWORD';
GO

-- restore the key with encryption password
USE MASTER
GO
CREATE CERTIFICATE TDECert
FROM FILE = 'C:\backup\TDE_Cert'
WITH PRIVATE KEY (FILE = 'C:\backup\TDECert_Key.pvk',
DECRYPTION BY PASSWORD = 'STRONGPASSWORD' );

Posted in Development | Tagged , | Leave a comment

TCPDF display chinese characters

There is a way to embed a TTF that can display Chinese characters.

Developer can use a font that:

  • Can display Chinese
  • Can be embedded to PDF

The fonts Droid Sans Fallback, which is free.

  1. copy the font to ./lib/TCPDF/fonts/DroidSansFallback.ttf
  2. use the below sample code in using the Droid Sans Fallback to output chinese characters
TCPDF_FONTS::addTTFfont('./lib/TCPDF/fonts/DroidSansFallback.ttf', 'TrueTypeUnicode', '', 32);
$pdf->SetFont('DroidSansFallback', '', 20);
$html = '中文字';
$pdf->SetFont('DroidSansFallback', '', 20);
$pdf->writeHTML($html, true, false, false, false, '');
$pdf->SetFont('DroidSansFallback', '', 10);
$pdf->writeHTML($html, true, false, false, false, '');
$pdf->SetFont('DroidSansFallback', '', 8);
$pdf->writeHTML($html, true, false, false, false, '');
Posted in Development | Tagged , , | Leave a comment

Synology raid rebuild speed

In case of slow raid rebuild speed, there are options to speed up the rebuild time.

ssh into the synology and use the below commands

echo 300000 > /proc/sys/dev/raid/speed_limit_min
echo 32768 > /sys/block/md2/md/stripe_cache_size
Posted in Development | Tagged , | Leave a comment

Synology WOL script

Wake On Lan Software - We've Compiled the BEST Tools for WOL in 2021

The script that will wake up another Synology machine, this enable me to wake up the machine before the backup script start.

usr/syno/sbin/synonet --wake [MAC address] eth0

Posted in Development | Tagged , , , | Leave a comment