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()查看

使用扩展中的函数

$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 繁体(台湾正体标准)到简体并转换为中国大陆常用词汇

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

  1. php 简繁体转换类库,OpenCC for PHP 优雅的简繁体转换

    写的第一个 PHP扩展,简单实用,使用中遇到 问题 和 bug 欢迎发 pull request 和 issue .当然不要忘记了支持点下 star :smile: 介绍 opencc4php 是 O ...

  2. java utf8 简繁转换 类库_在Java中进行中文繁体简体转换,基于OpenCC(Open Chinese Convert)方案...

    一.OpenCC介绍 OpenCC (Open Chinese Convert,开放中文转换) 是一个用于中文简繁转换的开源项目,支持词汇级别的转换.异体字转换和地区习惯用词转换(中国大陆.台湾.香港 ...

  3. php 简繁体转换类库,OpenCC for PHP 优雅的简繁体转换

    写的第一个 PHP扩展,简单实用,使用中遇到 问题 和 bug 欢迎发 pull request 和 issue .当然不要忘记了支持点下 star :smile: GitHUB: https://g ...

  4. java utf8 简繁转换 类库_Java封装简体繁体(香港台湾)转换工具

    简体繁体转换 新建工具类:HKTWwordVO import java.io.BufferedReader; import java.io.FileNotFoundException; import ...

  5. node 简繁体转换_编译了wasm版本的OpenCC,在浏览器上直接转换简繁体

    wasm-opencc开放中文转换OpenCC的wasm版本. 这个项目对OpenCC进行了添加修改修改,并利用Emscripten进行编译,在OpenCC进行中文简繁体转换的能力上具有以下特性:可在 ...

  6. java utf8 简繁转换 类库_JAVA简繁转换(对象)解决办法

    JAVA简繁转换(对象) 目前已实现普通字符串的转换代码如下: static String jtPy = "简体字库"; static String ftPy = "繁體 ...

  7. 台湾繁体转换成大陆繁体的简单方法

    问题: 在繁体网站下的txt格式的文章,或者繁体版游戏的说明文件,打开看的时候看到的却是乱码, 在没有工具把它还原回来的情况下,能不能转换成大陆繁体文字并保存下来. 不是简体哦,简体用软件很容易就实现 ...

  8. java utf8 简繁转换 类库,java 中文繁简体转换工具 opencc4j

    创作缘由 对于中文的繁简体转换是一种很常见的需求. 但是很多工具类都是简单的做个映射.(使用map,集合,properties)等. 存在一个严重的问题:特殊词组 的转换可能存在问题. OpenCC ...

  9. java utf8 简繁转换 类库_java简繁转换(区分港台)

    java工具类,支持中文简体转香港繁体.台湾繁体,繁体转简体: 代码如下: package translate; import java.io.BufferedReader; import java. ...

最新文章

  1. pandas计算滑动窗口中的数值总和实战(Rolling Sum of a Pandas Column):计算单数据列滑动窗口中的数值总和(sum)、计算多数据列滑动窗口中的数值总和(sum)
  2. python使用imbalanced-learn的AllKNN方法进行下采样处理数据不平衡问题
  3. Part1_1 pycharm的一些基础设置
  4. 学习笔记:Zookeeper选举机制
  5. JavaScript:undefined And null差异
  6. linux shell 脚本 supress,Linux指令和shell脚本
  7. 疯子的算法总结(六) 简单排序总 选择排序+插入排序+比较排序+冒泡排序
  8. json字符串生成C#实体类的工具
  9. tf.gfile()函数
  10. postgresql分割字符串_postgresql 实现字符串分割字段转列表查询
  11. APP启动页HTML,启动页.html
  12. 【NOIP】OpenJudge - 15:银行利息
  13. Mysql中怎样创建和使用存储过程
  14. iOS 一种很方便的构造TarBar
  15. MacOS Big Sur 11.5 (20G71) OC 0.7.1 / Cl 5138 / PE 三分区原版黑苹果镜像
  16. android 自定义快捷键,Android Studio中常用设置与快捷键
  17. SpringBoot 3.0最低版本要求的JDK 17,这几个新特性不能不知道
  18. Stream Collectors - counting
  19. C语言 键盘编码 及 用法
  20. VR专业名字解释_羊豆豆_新浪博客

热门文章

  1. jQuery-select2 官方文档笔记(二)——较高级应用
  2. 女生转行IT和汉子要考虑的问题有什么不一样?
  3. 深度学习优化算法:RMSProp算法
  4. 2022杭电多校(一)
  5. CountDownLatch导致的线程阻塞问题及线程池的使用
  6. C++ 数组、数组指针、指针数组、动态数组等详解
  7. go中的堆栈与C++中的堆栈
  8. Linux学习:Linux的发展历史及特点
  9. shell 整型变量定义
  10. EnterCriticalSection讲解