我找到了一个 PHP 脚本,可以在某些重音字符中查找文本。我的项目是 nodejs+mongodb,所以我尝试翻译 JavaScript,但我根本无法翻译。由于我不太了解PHP,需要一些帮助来翻译。
PHP Script 源代码来自http://tech.rgou.net/en/php/pesquisas-nao-sensiveis-ao-caso-e-acento-no-mongodb-e-php/
/**
* Description of StringUtil
*
* @author Rafael Goulart
*/
class StringUtil {
const ACCENT_STRINGS = 'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËẼÌÍÎÏĨÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëẽìíîïĩðñòóôõöøùúûüýÿ';
const NO_ACCENT_STRINGS = 'SOZsozYYuAAAAAAACEEEEEIIIIIDNOOOOOOUUUUYsaaaaaaaceeeeeiiiiionoooooouuuuyy';
/**
* Returns a string with accent to REGEX expression to find any combinations
* in accent insentive way
*
* @param string $text The text.
* @return string The REGEX text.
*/
static public function accentToRegex($text)
{
$from = str_split(utf8_decode(self::ACCENT_STRINGS));
$to = str_split(strtolower(self::NO_ACCENT_STRINGS));
$text = utf8_decode($text);
$regex = array();
foreach ($to as $key => $value)
{
if (isset($regex[$value]))
{
$regex[$value] .= $from[$key];
} else {
$regex[$value] = $value;
}
}
foreach ($regex as $rg_key => $rg)
{
$text = preg_replace("/[$rg]/", "_{$rg_key}_", $text);
}
foreach ($regex as $rg_key => $rg)
{
$text = preg_replace("/_{$rg_key}_/", "[$rg]", $text);
}
return utf8_encode($text);
}
}
这是我需要翻译的 JavaScript 代码......任何帮助都会得到帮助!谢谢。
function accentToRegex(word){
var ACCENT_STRINGS = 'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËẼÌÍÎÏĨÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëẽìíîïĩðñòóôõöøùúûüýÿ';
var NO_ACCENT_STRINGS = 'SOZsozYYuAAAAAAACEEEEEIIIIIDNOOOOOOUUUUYsaaaaaaaceeeeeiiiiionoooooouuuuyy';
var from = (ACCENT_STRINGS).split(decode_utf8(ACCENT_STRINGS));
var to = NO_ACCENT_STRINGS.split(NO_ACCENT_STRINGS.toLocaleLowerCase());
var text = decode_utf8(word);
var regex = new Array();
for(value in to)
{
if (!regex[value])
{
regex[value] = from[value];
} else {
regex[value] = value;
}
}
for (rg_key in regex)
{
// "$TESTONE $TESTONE".replace( new RegExp("\\$TESTONE","gm"),"foo")
// text = preg_replace("/[$rg]/", "_{$rg_key}_", $text);
text = text.replace(new RegExp(/[rg]/), new RegExp(_{rg_key}_)) ;
}
foreach (rg in regex)
{
// $text = preg_replace("/_{$rg_key}_/", "[$rg]", $text);
text = text.replace(new RegExp(/[rg]/), new RegExp(_{rg_key}_)) ;
}
return encode_utf8(text); //Edited from $text to text
}
function encode_utf8(s) {
return encodeURIComponent(s);
}
function decode_utf8(s) {
return decodeURIComponent(s);
}
错误是