PHP 正则表达式 Preg_replace 希伯来语 Unicode 到另一个 Unicode

PHP Regex Preg_replace Hebrew Unicode to another Unicode

我需要将 3 个希伯来语 Unicode 字符替换为另外 3 个希伯来语 Unicode 字符。我查看了 PHP 语法并进行了搜索,但这是我能写的最好的。它可以正常工作。

我想知道并想知道这是否是在 PHP 中将一个 Unicode 字符替换为另一个 Unicode 字符的最佳方法,然后再将其变成一个小函数。

PHP 中有更好的语法吗?

$re1 = '/[\x{05B1}]/u';
$re2 = '/[\x{05B2}]/u';
$re3 = '/[\x{05B3}]/u';

$subst1 = json_decode('"\u05B6"');
$subst2 = json_decode('"\u05B0"');
$subst3 = json_decode('"\u05B8"');

//Replace (Niqqud with Cantillation) with (just Niqqud)
$bible_content = preg_replace($re1, $subst1, $bible_content);
$bible_content = preg_replace($re2, $subst2, $bible_content);
$bible_content = preg_replace($re3, $subst3, $bible_content);

开始输入 $bible_content:

上帝称光为昼,称暗为夜,有一天有晚上,有早晨。 不从恶人的计谋,不从罪人的道路站住,不在忿怒的座位上坐下的,这人便为有福。 Hֳ

$bible_content 的预期输出:

上帝称光为昼,称暗为夜,有一天有晚上,有早晨。 不从恶人的计谋,不从罪人的道路站住,不在忿怒的座位上坐下的,这人便为有福。 啊

PHP 7.0 为字符串文字中的 unicode 字符提供了新语法。此外,您可以使用 strtr 函数来处理字符到字符的替换。

$from = "\u{05B1}\u{05B2}\u{05B3}";
$to = "\u{05B6}\u{05B0}\u{05B8}";

echo strtr($bible_content, $from, $to). "\n";

现在,我看不懂希伯来语(甚至不能让它在 RTL 中正常流动,显然 :P ),所以你必须判断它是否做了正确的事情。