承接 atk14/translate 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

atk14/translate

最新稳定版本:v1.2.3

Composer 安装命令:

composer require atk14/translate

包简介

A PHP class for converting strings between character encodings without relying on iconv

README 文档

README

Tests

A PHP class for converting strings between character encodings, without relying on the iconv extension.

Installation

composer require atk14/translate

Usage

Converting between encodings

$iso = Translate::Trans($utf8_string, "UTF-8", "ISO-8859-2");
$utf8 = Translate::Trans($iso_string, "ISO-8859-2", "UTF-8");
$win  = Translate::Trans($utf8_string, "UTF-8", "Windows-1250");

If a conversion is not supported, the original string is returned unchanged.

Converting arrays

When $text is an array, all string values are converted recursively. Non-string values (integers, nulls, ...) are left as-is.

$data = ["name" => "Žofie", "age" => 42];
$converted = Translate::Trans($data, "UTF-8", "ISO-8859-2");

To also convert array keys, pass the recode_array_keys option:

$converted = Translate::Trans($data, "UTF-8", "ISO-8859-2", ["recode_array_keys" => true]);

Transliteration to ASCII

echo Translate::Trans("Příliš žluťoučký kůň", "UTF-8", "ASCII");
// → "Prilis zlutoucky kun"

The language option resolves conflicts between transliteration rules for different languages. Default is Slovak (suitable for Czech and Slovak texts).

// Slovak (default): ä → a
echo Translate::Trans("mäso", "UTF-8", "ASCII");
// → "maso"

// German: ä → ae
echo Translate::Trans("Jäger", "UTF-8", "ASCII", ["language" => "de"]);
// → "Jaeger"

Accepted language values: "sk", "sk_SK", "de", "de_DE", "DE", "en", etc.

Case conversion

echo Translate::Lower("PŘÍLIŠ", "UTF-8");        // → "příliš"
echo Translate::Upper("žluťoučký", "UTF-8");     // → "ŽLUŤOUČKÝ"

echo Translate::Lower("PŘÍLIŠ", "ISO-8859-2");   // works for 8-bit encodings too
echo Translate::Upper("žluťoučký", "Windows-1250");

Checking encoding

Returns true if the string is valid in the given encoding, false otherwise.

Translate::CheckEncoding($string, "UTF-8");   // → true / false
Translate::CheckEncoding($string, "ASCII");   // → true / false

Also works with arrays (checks all keys and values recursively):

Translate::CheckEncoding(["key" => "value", ...], "UTF-8");

An optional third argument lists forbidden byte sequences:

Translate::CheckEncoding($string, "UTF-8", ["\r\n", "<script"]);

String length

Returns the number of characters (not bytes) for multi-byte encodings:

$len = Translate::Length("Příliš", "UTF-8");   // → 6
$len = Translate::Length("Prilis", "ASCII");   // → 6

Supported encodings

Encoding Aliases
UTF-8 utf-8
UTF-16 utf-16
ISO-8859-1 latin1, latin-1, lat1, il1, 8859_1, ...
ISO-8859-2 latin2, latin-2, lat2, il2, 8859_2, ...
Windows-1250 cp1250, win-1250, 1250, ...
Windows-1252 cp1252, win-1252, 1252, ...
CP852 cp852, ibm852, cp-852, ...
ASCII us-ascii, usascii
HTML entities html entities
KOI8 koi-8, cskoi8r
Legacy kam, mac, macce, pc2, pc2a, vga

Charset names are case-insensitive.

Using iconv

By default, the class uses its own conversion tables. If you are confident that iconv works correctly on your system (verify by running the test suite), you can enable it for better performance:

define("TRANSLATE_USE_ICONV", true);

Testing

Translate is tested automatically via GitHub Actions across PHP 5.6 to PHP 8.5.

Tests use the atk14/tester wrapper for phpunit/phpunit.

Install development dependencies:

composer update --dev

Run the test suite:

./vendor/bin/run_unit_tests test

License

Translate is free software distributed under the terms of the MIT license.

统计信息

  • 总下载量: 38.68k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固