如何用 PHP 替换字符串中的 Hebrew Cantillation(不是 Nikkud)字符?
How to replace Hebrew Cantillation (not Nikkud) characters from string with PHP?
我想从希伯来语字符串中去除 Cantillation,而不是 Nikkud。
我找到了这个 JS 代码。我如何在 PHP 中执行此操作?
function stripCantillation(str){
return str.replace(/[\u0591-\u05AF]/g,"").replace("׀", "").replace("׃","").replace("־","");
}
带旋律的希伯来语文本
17
没有旋律的希伯来语文本
起初神创造天地
这是包含所有 unicode 字符的 php 友好正则表达式模式:
/[\x{0591}-\x{05AF}\x{05BE}\x{05C0}\x{05C3}]/u
(Pattern Demo)
为了表达这些 unicode 字符,将 4 个字符的代码括在大括号 {}
中并在前面加上 \x
。 u
标志必须跟在表达式后面。字符 class 的内容(在方括号 []
之间)以一系列字符开头,后跟三个单独的字符。
以下代码段将使用 php 执行正则表达式模式,并根据是否实际进行了任何替换来显示输出。当然,如果你不需要计算替换,你可以只用preg_replace()
中的return值重新声明输入字符串并省略第3和第4个参数。
代码(Demo):
$inputs = [
'בְּרֵאשִׁית בָּרָא אֱלֹהִים אֵת הַשָּׁמַיִם וְאֵת הָאָֽרֶץ',
'בְּרֵאשִׁ֖ית בָּרָ֣א אֱלֹהִ֑ים אֵ֥ת הַשָּׁמַ֖יִם וְאֵ֥ת הָאָֽרֶץ',
];
foreach ($inputs as $input) {
$output = preg_replace('/[\x{0591}-\x{05AF}\x{05BE}\x{05C0}\x{05C3}]+/u', '', $input, -1, $count);
echo !$count ? "no change" : "Replacement Count: {$count}\nBefore: {$input}\n After: {$output}";
echo "\n---\n";
}
输出:
no change
---
Replacement Count: 6
Before: בְּרֵאשִׁ֖ית בָּרָ֣א אֱלֹהִ֑ים אֵ֥ת הַשָּׁמַ֖יִם וְאֵ֥ת הָאָֽרֶץ
After: בְּרֵאשִׁית בָּרָא אֱלֹהִים אֵת הַשָּׁמַיִם וְאֵת הָאָֽרֶץ
---
这是将被替换的突出显示 table 字符:
图片来源:http://unicode.org/charts/PDF/U0590.pdf
我想从希伯来语字符串中去除 Cantillation,而不是 Nikkud。 我找到了这个 JS 代码。我如何在 PHP 中执行此操作?
function stripCantillation(str){
return str.replace(/[\u0591-\u05AF]/g,"").replace("׀", "").replace("׃","").replace("־","");
}
带旋律的希伯来语文本
17没有旋律的希伯来语文本
起初神创造天地
这是包含所有 unicode 字符的 php 友好正则表达式模式:
/[\x{0591}-\x{05AF}\x{05BE}\x{05C0}\x{05C3}]/u
(Pattern Demo)
为了表达这些 unicode 字符,将 4 个字符的代码括在大括号 {}
中并在前面加上 \x
。 u
标志必须跟在表达式后面。字符 class 的内容(在方括号 []
之间)以一系列字符开头,后跟三个单独的字符。
以下代码段将使用 php 执行正则表达式模式,并根据是否实际进行了任何替换来显示输出。当然,如果你不需要计算替换,你可以只用preg_replace()
中的return值重新声明输入字符串并省略第3和第4个参数。
代码(Demo):
$inputs = [
'בְּרֵאשִׁית בָּרָא אֱלֹהִים אֵת הַשָּׁמַיִם וְאֵת הָאָֽרֶץ',
'בְּרֵאשִׁ֖ית בָּרָ֣א אֱלֹהִ֑ים אֵ֥ת הַשָּׁמַ֖יִם וְאֵ֥ת הָאָֽרֶץ',
];
foreach ($inputs as $input) {
$output = preg_replace('/[\x{0591}-\x{05AF}\x{05BE}\x{05C0}\x{05C3}]+/u', '', $input, -1, $count);
echo !$count ? "no change" : "Replacement Count: {$count}\nBefore: {$input}\n After: {$output}";
echo "\n---\n";
}
输出:
no change
---
Replacement Count: 6
Before: בְּרֵאשִׁ֖ית בָּרָ֣א אֱלֹהִ֑ים אֵ֥ת הַשָּׁמַ֖יִם וְאֵ֥ת הָאָֽרֶץ
After: בְּרֵאשִׁית בָּרָא אֱלֹהִים אֵת הַשָּׁמַיִם וְאֵת הָאָֽרֶץ
---
这是将被替换的突出显示 table 字符: 图片来源:http://unicode.org/charts/PDF/U0590.pdf