试图用PHP正则表达式生成url段塞,日语字符无法通过
所以我试图生成slug来存储在我的数据库中。我所在的地区包括英语、一些欧洲语言和日语 我允许试图用PHP正则表达式生成url段塞,日语字符无法通过,php,regex,Php,Regex,所以我试图生成slug来存储在我的数据库中。我所在的地区包括英语、一些欧洲语言和日语 我允许\d,\w,欧洲字符是音译的,日本字符是不动的。保留句号、加号和破折号(-)。删除前导/尾随空格,而中间的空格替换为破折号 以下是一些代码:(请随意改进它,考虑到我的上述条件,因为我的正则表达式fu目前是白带层) 我正在测试这个函数,但是我的JP字符没有通过,它们只是被'替换。虽然我怀疑是//IGNORE把它们删掉了,但我需要的是,它们或我的德语、法语的音译将不起作用。有没有办法解决这个问题 编辑:我不确
\d
,\w
,欧洲字符是音译的,日本字符是不动的。保留句号、加号和破折号(-
)。删除前导/尾随空格,而中间的空格替换为破折号
以下是一些代码:(请随意改进它,考虑到我的上述条件,因为我的正则表达式fu目前是白带层)
我正在测试这个函数,但是我的JP字符没有通过,它们只是被'
替换。虽然我怀疑是//IGNORE
把它们删掉了,但我需要的是,它们或我的德语、法语的音译将不起作用。有没有办法解决这个问题
编辑:我不确定日本汉字是否涵盖了所有的简体中文,但我需要它和韩文。如果任何人谁知道的正则表达式的蝙蝠请让我知道这将节省我一些时间搜索。谢谢。注意:我不熟悉日语的书写系统
查看函数,iconv
调用将删除所有日语字符。与其使用iconv
进行音译,只需创建一个函数即可:
function _toSlugTransliterate($string) {
// Lowercase equivalents found at:
// https://github.com/kohana/core/blob/3.3/master/utf8/transliterate_to_ascii.php
$lower = [
'à'=>'a','ô'=>'o','ď'=>'d','ḟ'=>'f','ë'=>'e','š'=>'s','ơ'=>'o',
'ß'=>'ss','ă'=>'a','ř'=>'r','ț'=>'t','ň'=>'n','ā'=>'a','ķ'=>'k',
'ŝ'=>'s','ỳ'=>'y','ņ'=>'n','ĺ'=>'l','ħ'=>'h','ṗ'=>'p','ó'=>'o',
'ú'=>'u','ě'=>'e','é'=>'e','ç'=>'c','ẁ'=>'w','ċ'=>'c','õ'=>'o',
'ṡ'=>'s','ø'=>'o','ģ'=>'g','ŧ'=>'t','ș'=>'s','ė'=>'e','ĉ'=>'c',
'ś'=>'s','î'=>'i','ű'=>'u','ć'=>'c','ę'=>'e','ŵ'=>'w','ṫ'=>'t',
'ū'=>'u','č'=>'c','ö'=>'o','è'=>'e','ŷ'=>'y','ą'=>'a','ł'=>'l',
'ų'=>'u','ů'=>'u','ş'=>'s','ğ'=>'g','ļ'=>'l','ƒ'=>'f','ž'=>'z',
'ẃ'=>'w','ḃ'=>'b','å'=>'a','ì'=>'i','ï'=>'i','ḋ'=>'d','ť'=>'t',
'ŗ'=>'r','ä'=>'a','í'=>'i','ŕ'=>'r','ê'=>'e','ü'=>'u','ò'=>'o',
'ē'=>'e','ñ'=>'n','ń'=>'n','ĥ'=>'h','ĝ'=>'g','đ'=>'d','ĵ'=>'j',
'ÿ'=>'y','ũ'=>'u','ŭ'=>'u','ư'=>'u','ţ'=>'t','ý'=>'y','ő'=>'o',
'â'=>'a','ľ'=>'l','ẅ'=>'w','ż'=>'z','ī'=>'i','ã'=>'a','ġ'=>'g',
'ṁ'=>'m','ō'=>'o','ĩ'=>'i','ù'=>'u','į'=>'i','ź'=>'z','á'=>'a',
'û'=>'u','þ'=>'th','ð'=>'dh','æ'=>'ae','µ'=>'u','ĕ'=>'e','ı'=>'i',
];
return str_replace(array_keys($lower), array_values($lower), $string);
}
因此,经过一些修改,它可能看起来像这样:
function toSlug($string, $separator = '-') {
// Work around this...
#$string = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $string);
$string = _toSlugTransliterate($string);
// Remove unwanted chars + trim excess whitespace
// I got the character ranges from the following URL:
// https://stackoverflow.com/questions/6787716/regular-expression-for-japanese-characters#10508813
$regex = '/[^一-龠ぁ-ゔァ-ヴーa-zA-Z0-9a-zA-Z0-9々〆〤.+ -]|^\s+|\s+$/u';
$string = preg_replace($regex, '', $string);
// Using the mb_* version seems safer for some reason
$string = mb_strtolower($string);
// Same as before
$string = preg_replace("/[ {$separator}]+/", $separator, $string);
return $string;
}
$x = ' æøå!this.ís-a test-ゔヴ ーァ ';
echo toSlug($x);
'/[\p{Hiragana}\p{Katakana}\p{Han}]+/'
在正则表达式中,可以使用unicode“脚本”匹配各种语言的字母。没有“日语”一词,但有平假名
,片假名
和韩语
。因为我不知道日语是怎么写的,也不知道怎么用,所以我甚至不打算尝试
但是,使用这些脚本可以执行如下操作:
function toSlug($string, $separator = '-') {
// Work around this...
#$string = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $string);
$string = _toSlugTransliterate($string);
// Remove unwanted chars + trim excess whitespace
// I got the character ranges from the following URL:
// https://stackoverflow.com/questions/6787716/regular-expression-for-japanese-characters#10508813
$regex = '/[^一-龠ぁ-ゔァ-ヴーa-zA-Z0-9a-zA-Z0-9々〆〤.+ -]|^\s+|\s+$/u';
$string = preg_replace($regex, '', $string);
// Using the mb_* version seems safer for some reason
$string = mb_strtolower($string);
// Same as before
$string = preg_replace("/[ {$separator}]+/", $separator, $string);
return $string;
}
$x = ' æøå!this.ís-a test-ゔヴ ーァ ';
echo toSlug($x);
'/[\p{Hiragana}\p{Katakana}\p{Han}]+/'
这个答案有帮助吗:因为“获取帮助”可以完美地工作。我正在处理数十万条记录,所以性能可能会受到一些影响。