php 简繁体转换类库,简繁体转化处理 php+opencc 扩展

opencc4php 是 OpenCC 的PHP扩展,能很智能的完成简繁体转换

先clone下来

root@tony:/usr/local# git clone https://github.com/BYVoid/OpenCC.git

Cloning into 'OpenCC'...

remote: Counting objects: 6344, done.

remote: Total 6344 (delta 0), reused 0 (delta 0), pack-reused 6344

Receiving objects: 100% (6344/6344), 5.12 MiB | 21.00 KiB/s, done.

Resolving deltas: 100% (4170/4170), done.

Checking connectivity... done.

进入目录,开始编译

root@tony:/usr/local# cd OpenCC/

root@tony:/usr/local/OpenCC# ls

AUTHORS data LICENSE node README.md

binding.gyp deps Makefile opencc.pc.in src

CMakeLists.txt doc NEWS.md package.json test

//执行make,这时候发生错误

root@tony:/usr/local/OpenCC# make

mkdir -p build/rel

(cd build/rel; cmake \

-DBUILD_DOCUMENTATION:BOOL=ON \

-DENABLE_GTEST:BOOL=OFF \

-DCMAKE_BUILD_TYPE=Release \

-DCMAKE_INSTALL_PREFIX=/usr \

../..)

/bin/sh: 1: cmake: not found

Makefile:24: recipe for target 'build' failed

make: *** [build] Error 127

这是由于未安装cmake,立刻安装

root@tony:/usr/local/OpenCC# apt-get install cmake

Reading package lists... Done

Building dependency tree

Reading state information... Done

Package cmake is not available, but is referred to by another package.

This may mean that the package is missing, has been obsoleted, or

is only available from another source

E: Package 'cmake' has no installation candidate

如果源找不到,可添加新的源进去

nano /etc/apt/sources.list

deb http://ftp.cn.debian.org/debian/ jessie main

deb-src http://ftp.cn.debian.org/debian/ jessie main

apt-get update

再次安装

root@tony:/usr/local/OpenCC# apt-get install cmake

Reading package lists... Done

Building dependency tree

Reading state information... Done

Note, selecting 'cmake' for regex 'cmake*'

0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.

root@tony:/usr/local/OpenCC# apt-get update

Ign http://ftp.cn.debian.org jessie InRelease

Get:1 http://ftp.cn.debian.org jessie Release.gpg [2,373 B]

Get:2 http://ftp.cn.debian.org jessie Release [148 kB]

Hit http://security.debian.org jessie/updates InRelease

Get:3 http://ftp.cn.debian.org jessie/main Sources [7,057 kB]

Hit http://security.debian.org jessie/updates/main Sources

Hit http://security.debian.org jessie/updates/main amd64 Packages

Get:4 http://ftp.debian.org jessie-updates InRelease [142 kB]

Hit http://security.debian.org jessie/updates/main Translation-en

Get:5 http://ftp.cn.debian.org jessie/main amd64 Packages [6,761 kB]

Get:6 http://ftp.cn.debian.org jessie/main Translation-en [4,580 kB]

Get:7 http://ftp.debian.org jessie-updates/main Sources [15.5 kB]

Get:8 http://ftp.cn.debian.org jessie/main Translation-zh [1,526 B]

Get:9 http://ftp.cn.debian.org jessie/main Translation-zh_CN [101 kB]

Get:10 http://ftp.debian.org jessie-updates/main Translation-en [12.6 kB]

Get:11 http://ftp.debian.org jessie-updates/main amd64 Packages [15.5 kB]

Fetched 18.8 MB in 6s (2,742 kB/s)

Reading package lists... Done

root@tony:/usr/local/OpenCC# apt-get install cmake

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following extra packages will be installed:

cmake-data libarchive13 libcurl3 liblzo2-2

Suggested packages:

codeblocks eclipse ninja-build lrzip

The following NEW packages will be installed:

cmake cmake-data libarchive13 libcurl3 liblzo2-2

0 upgraded, 5 newly installed, 0 to remove and 4 not upgraded.

Need to get 3,896 kB of archives.

After this operation, 18.1 MB of additional disk space will be used.

Do you want to continue? [Y/n] y

Get:1 http://ftp.cn.debian.org/debian/ jessie/main cmake-data all 3.0.2-1 [929 kB]

Get:2 http://security.debian.org/ jessie/updates/main libcurl3 amd64 7.38.0-4+deb8u4 [259 kB]

Get:3 http://ftp.cn.debian.org/debian/ jessie/main liblzo2-2 amd64 2.08-1.2 [54.6 kB]

Get:4 http://ftp.cn.debian.org/debian/ jessie/main libarchive13 amd64 3.1.2-11+deb8u1 [269 kB]

Get:5 http://ftp.cn.debian.org/debian/ jessie/main cmake amd64 3.0.2-1 [2,384 kB]

Fetched 3,896 kB in 1s (2,225 kB/s)

Selecting previously unselected package cmake-data.

(Reading database ... 32716 files and directories currently installed.)

Preparing to unpack .../cmake-data_3.0.2-1_all.deb ...

Unpacking cmake-data (3.0.2-1) ...

Selecting previously unselected package liblzo2-2:amd64.

Preparing to unpack .../liblzo2-2_2.08-1.2_amd64.deb ...

Unpacking liblzo2-2:amd64 (2.08-1.2) ...

Selecting previously unselected package libarchive13:amd64.

Preparing to unpack .../libarchive13_3.1.2-11+deb8u1_amd64.deb ...

Unpacking libarchive13:amd64 (3.1.2-11+deb8u1) ...

Selecting previously unselected package libcurl3:amd64.

Preparing to unpack .../libcurl3_7.38.0-4+deb8u4_amd64.deb ...

Unpacking libcurl3:amd64 (7.38.0-4+deb8u4) ...

Selecting previously unselected package cmake.

Preparing to unpack .../cmake_3.0.2-1_amd64.deb ...

Unpacking cmake (3.0.2-1) ...

Processing triggers for man-db (2.7.0.2-5) ...

Setting up cmake-data (3.0.2-1) ...

Setting up liblzo2-2:amd64 (2.08-1.2) ...

Setting up libarchive13:amd64 (3.1.2-11+deb8u1) ...

Setting up libcurl3:amd64 (7.38.0-4+deb8u4) ...

Setting up cmake (3.0.2-1) ...

Processing triggers for libc-bin (2.19-18+deb8u4) ...

再次编译,再次报错

root@tony:/usr/local/OpenCC# make

mkdir -p build/rel

(cd build/rel; cmake \

-DBUILD_DOCUMENTATION:BOOL=ON \

-DENABLE_GTEST:BOOL=OFF \

-DCMAKE_BUILD_TYPE=Release \

-DCMAKE_INSTALL_PREFIX=/usr \

../..)

-- The CXX compiler identification is GNU 4.9.2

-- Check for working CXX compiler: /usr/bin/c++

-- Check for working CXX compiler: /usr/bin/c++ -- works

-- Detecting CXX compiler ABI info

-- Detecting CXX compiler ABI info - done

-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY

-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY - Success

-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY

-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY - Success

-- Performing Test COMPILER_HAS_DEPRECATED_ATTR

-- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Success

-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)

CMake Error at doc/CMakeLists.txt:4 (message):

Doxygen is needed to build the documentation. Please install it correctly

-- Configuring incomplete, errors occurred!

See also "/usr/local/OpenCC/build/rel/CMakeFiles/CMakeOutput.log".

Makefile:24: recipe for target 'build' failed

make: *** [build] Error 1

这次是缺少doxygen,也安装上

root@tony:/usr/local/OpenCC# apt-get install doxygen

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following extra packages will be installed:

libclang1-3.5 libllvm3.5 libobjc-4.9-dev libobjc4

Suggested packages:

doxygen-latex doxygen-doc doxygen-gui graphviz

The following NEW packages will be installed:

doxygen libclang1-3.5 libllvm3.5 libobjc-4.9-dev libobjc4

0 upgraded, 5 newly installed, 0 to remove and 4 not upgraded.

Need to get 14.5 MB of archives.

After this operation, 58.5 MB of additional disk space will be used.

Do you want to continue? [Y/n] y

Get:1 http://ftp.cn.debian.org/debian/ jessie/main libllvm3.5 amd64 1:3.5-10 [7,607 kB]

21% [1 libllvm3.5 3,021 kB/7,607 kB 40%]

Get:2 http://ftp.cn.debian.org/debian/ jessie/main libllvm3.5 amd64 1:3.5-10 [7,607 kB]

Get:3 http://ftp.cn.debian.org/debian/ jessie/main libobjc4 amd64 4.9.2-10 [112 kB]

Get:4 http://ftp.cn.debian.org/debian/ jessie/main libobjc-4.9-dev amd64 4.9.2-10 [372 kB]

Get:5 http://ftp.cn.debian.org/debian/ jessie/main libclang1-3.5 amd64 1:3.5-10 [3,654 kB]

Get:6 http://ftp.cn.debian.org/debian/ jessie/main doxygen amd64 1.8.8-5 [2,763 kB]

Fetched 11.5 MB in 2min 2s (93.6 kB/s)

Selecting previously unselected package libllvm3.5:amd64.

(Reading database ... 34424 files and directories currently installed.)

Preparing to unpack .../libllvm3.5_1%3a3.5-10_amd64.deb ...

Unpacking libllvm3.5:amd64 (1:3.5-10) ...

Selecting previously unselected package libobjc4:amd64.

Preparing to unpack .../libobjc4_4.9.2-10_amd64.deb ...

Unpacking libobjc4:amd64 (4.9.2-10) ...

Selecting previously unselected package libobjc-4.9-dev:amd64.

Preparing to unpack .../libobjc-4.9-dev_4.9.2-10_amd64.deb ...

Unpacking libobjc-4.9-dev:amd64 (4.9.2-10) ...

Selecting previously unselected package libclang1-3.5:amd64.

Preparing to unpack .../libclang1-3.5_1%3a3.5-10_amd64.deb ...

Unpacking libclang1-3.5:amd64 (1:3.5-10) ...

Selecting previously unselected package doxygen.

Preparing to unpack .../doxygen_1.8.8-5_amd64.deb ...

Unpacking doxygen (1.8.8-5) ...

Processing triggers for man-db (2.7.0.2-5) ...

Setting up libllvm3.5:amd64 (1:3.5-10) ...

Setting up libobjc4:amd64 (4.9.2-10) ...

Setting up libobjc-4.9-dev:amd64 (4.9.2-10) ...

Setting up libclang1-3.5:amd64 (1:3.5-10) ...

Setting up doxygen (1.8.8-5) ...

Processing triggers for libc-bin (2.19-18+deb8u4) ...

再次编译,OK了,再make install

### 接下来搞定opencc4php 扩展

```bash

root@tony:/usr/local# git clone https://github.com/NauxLiu/opencc4php.git

Cloning into 'opencc4php'...

remote: Counting objects: 168, done.

remote: Total 168 (delta 0), reused 0 (delta 0), pack-reused 168

Receiving objects: 100% (168/168), 28.38 KiB | 0 bytes/s, done.

Resolving deltas: 100% (90/90), done.

Checking connectivity... done.

进入目录,phpize添加扩展

root@tony:/usr/local# cd opencc4php/

root@tony:/usr/local/opencc4php# ls

config.m4 config.w32 CREDITS EXPERIMENTAL opencc.c opencc.php php_opencc.h README.md tests travis

root@tony:/usr/local/opencc4php# phpize

Configuring for:

PHP Api Version: 20131106

Zend Module Api No: 20131226

Zend Extension Api No: 220131226

如果你的OpenCC安装目录不在/usr或/usr/local,可在./configure时添加--with-opencc=[DIR]指定你的OpenCC目录

root@tony:/usr/local/opencc4php# ./configure

checking for grep that handles long lines and -e... /bin/grep

checking for egrep... /bin/grep -E

checking for a sed that does not truncate output... /bin/sed

checking for cc... cc

checking whether the C compiler works... yes

checking for C compiler default output file name... a.out

checking for suffix of executables...

checking whether we are cross compiling... no

checking for suffix of object files... o

checking whether we are using the GNU C compiler... yes

checking whether cc accepts -g... yes

checking for cc option to accept ISO C89... none needed

checking how to run the C preprocessor... cc -E

checking for icc... no

checking for suncc... no

checking whether cc understands -c and -o together... yes

checking for system library directory... lib

checking if compiler supports -R... no

checking if compiler supports -Wl,-rpath,... yes

checking build system type... x86_64-unknown-linux-gnu

checking host system type... x86_64-unknown-linux-gnu

checking target system type... x86_64-unknown-linux-gnu

checking for PHP prefix... /usr

checking for PHP includes... -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend -I/usr/include/php5/ext -I/usr/include/php5/ext/date/lib

checking for PHP extension directory... /usr/lib/php5/20131226

checking for PHP installed headers prefix... /usr/include/php5

checking if debug is enabled... no

checking if zts is enabled... no

checking for re2c... no

configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.

checking for gawk... no

checking for nawk... nawk

checking if nawk is broken... no

checking for opencc support... yes, shared

checking for opencc files in default path... found in /usr

checking how to print strings... printf

checking for a sed that does not truncate output... (cached) /bin/sed

checking for fgrep... /bin/grep -F

checking for ld used by cc... /usr/bin/ld

checking if the linker (/usr/bin/ld) is GNU ld... yes

checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B

checking the name lister (/usr/bin/nm -B) interface... BSD nm

checking whether ln -s works... yes

checking the maximum length of command line arguments... 1572864

checking whether the shell understands some XSI constructs... yes

checking whether the shell understands "+="... yes

checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop

checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop

checking for /usr/bin/ld option to reload object files... -r

checking for objdump... objdump

checking how to recognize dependent libraries... pass_all

checking for dlltool... no

checking how to associate runtime and link libraries... printf %s\n

checking for ar... ar

checking for archiver @FILE support... @

checking for strip... strip

checking for ranlib... ranlib

checking for gawk... (cached) nawk

checking command to parse /usr/bin/nm -B output from cc object... ok

checking for sysroot... no

checking for mt... mt

checking if mt is a manifest tool... no

checking for ANSI C header files... yes

checking for sys/types.h... yes

checking for sys/stat.h... yes

checking for stdlib.h... yes

checking for string.h... yes

checking for memory.h... yes

checking for strings.h... yes

checking for inttypes.h... yes

checking for stdint.h... yes

checking for unistd.h... yes

checking for dlfcn.h... yes

checking for objdir... .libs

checking if cc supports -fno-rtti -fno-exceptions... no

checking for cc option to produce PIC... -fPIC -DPIC

checking if cc PIC flag -fPIC -DPIC works... yes

checking if cc static flag -static works... yes

checking if cc supports -c -o file.o... yes

checking if cc supports -c -o file.o... (cached) yes

checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes

checking whether -lc should be explicitly linked in... no

checking dynamic linker characteristics... GNU/Linux ld.so

checking how to hardcode library paths into programs... immediate

checking whether stripping libraries is possible... yes

checking if libtool supports shared libraries... yes

checking whether to build shared libraries... yes

checking whether to build static libraries... no

configure: creating ./config.status

config.status: creating config.h

config.status: executing libtool commands

开始编译

root@tony:/usr/local/opencc4php# make test

Build complete.

Don't forget to run 'make test'.

=====================================================================

PHP : /usr/bin/php

PHP_SAPI : cli

PHP_VERSION : 5.6.24-0+deb8u1

ZEND_VERSION: 2.6.0

PHP_OS : Linux - Linux tony 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2+deb8u3 (2016-07-02) x86_64

INI actual : /usr/local/opencc4php/tmp-php.ini

More .INIs :

CWD : /usr/local/opencc4php

Extra dirs :

VALGRIND : Not used

=====================================================================

TIME START 2016-08-19 06:48:29

=====================================================================

PASS Check for opencc presence [tests/001.phpt]

=====================================================================

TIME END 2016-08-19 06:48:29

=====================================================================

TEST RESULT SUMMARY

---------------------------------------------------------------------

Exts skipped : 0

Exts tested : 43

---------------------------------------------------------------------

Number of tests : 1 1

Tests skipped : 0 ( 0.0%) --------

Tests warned : 0 ( 0.0%) ( 0.0%)

Tests failed : 0 ( 0.0%) ( 0.0%)

Expected fail : 0 ( 0.0%) ( 0.0%)

Tests passed : 1 (100.0%) (100.0%)

---------------------------------------------------------------------

Time taken : 0 seconds

=====================================================================

This report can be automatically sent to the PHP QA team at

http://qa.php.net/reports and http://news.php.net/php.qa.reports

This gives us a better understanding of PHP's behavior.

If you don't want to send the report immediately you can choose

option "s" to save it. You can then email it to qa-reports@lists.php.net later.

Do you want to send this report now? [Yns]: y

Please enter your email address.

(Your address will be mangled so that it will not go out on any

mailinglist in plain text):

Posting to http://qa.php.net/buildtest-process.php

Thank you for helping to make PHP better.

安装

root@tony:/usr/local/opencc4php# make install

Installing shared extensions: /usr/lib/php5/20131226/

再将扩展添加到.ini

//创建一个opencc.ini文件,写入保存

extension=opencc.so

再启动扩展,可以使用php5enmod工具

root@tony:/etc/php5/mods-available# php5enmod opencc

如果没有php5enmod工具,可以手动启用

ln -s /etc/php5/mods-available/opencc.ini /etc/php5/cli/conf.d/opencc.ini

ln -s /etc/php5/mods-available/opencc.ini /etc/php5/fpm/conf.d/opencc.ini

最后重启一下服务

root@tony:/etc/php5/mods-available# /etc/init.d/nginx restart

[ ok ] Restarting nginx (via systemctl): nginx.service.

root@tony:/etc/php5/mods-available# /etc/init.d/php5-fpm restart

[ ok ] Restarting php5-fpm (via systemctl): php5-fpm.service.

至此,安装工作全部完成,phpinfo()查看

a9d0b9241a27

使用扩展中的函数

$od = opencc_open("s2twp.json"); //传入配置文件名

$text = opencc_convert("我是简体。", $od);

echo $text;

opencc_close($od);

函数列表

opencc_open(string ConfigName) ConfigName:配置文件名,成功返回资源对象,失败返回false

opencc_close(resource ob) 关闭资源对象,成功返回true,失败返回false. 成功后od会置为NULL

opencc_error() 返回最后一条错误信息,有错误信息返回String,无错误返回false

opencc_convert(string str, resource od) str:要转换的字符串(UTF-8),od:opencc资源对象

可用配置

s2t.json 简体到繁体

t2s.json 繁体到简体

s2tw.json 简体到台湾正体

tw2s.json 台湾正体到简体

s2hk.json 简体到香港繁体(香港小学学习字词表标准)

hk2s.json 香港繁体(香港小学学习字词表标准)到简体

s2twp.json 简体到繁体(台湾正体标准)并转换为台湾常用词汇

tw2sp.json 繁体(台湾正体标准)到简体并转换为中国大陆常用词汇

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值