我一直在研究我的一个小项目,它可以将英语单词翻译成插入并从数据库中删除的生成单词。例如,“马铃薯”这个词就会变成“萌芽”(让我很开心)。
首先,我试图将整个英语词典翻译成我的替代语言,但这是一个缓慢的过程,所以我认为翻译这些词语会更容易。
用户可以将段落复制并粘贴到一个简单的html表单中,php会过滤掉单词并检查数据库中是否存在现有条目。如果没有php,那么将生成一个要使用的单词并将其插入数据库中以供日后使用。
目前我正在使用正则表达式来删除用户插入的单词,但我希望输出在处理完单词后保留标点符号。
$words = $_POST['long'];
$words = preg_replace('/\s+/', ' ', $words);
$words = preg_replace('/[^a-z]+/i', ' ', $words);
$words = trim($words);
$pieces = explode(" ", $words);
回到马铃薯的例子,如果我粘贴这段信息:"However, the local importance of the potato is variable and changing rapidly. It remains an essential crop in Europe (especially eastern and central Europe), where per capita production is still the highest in the world, but the most rapid expansion over the past few decades has occurred in southern and eastern Asia. As of 2007 China led the world in potato production, and nearly a third of the world's potatoes were harvested in China and India."
它将成为:ejijzow omr aqeri akfotwkapz ko omr budfun uo jwqixifp wka aidongfi ewmtejb pa dqnuuqs yh peuodvsuu dqte so ixespv gajhiqhxes gaoemqu wka ntebvze ixespv beqaj qze hipavw oaeiawlebu uo thvia omr koyjxuv so omr brquf lbs omr zukk hujze orijamzhi vunz omr aimr iqe vqgugfp uny eenecrod so vimeurbs wka gaoemqu dhyu ro ko yieqv ugn omr brquf so budfun oaeiawlebu wka xkpinl x vmitc ko omr brquf e fbecndix edpu agehbeano so yieqv wka olmog
正如您所看到的那样,由于上述过滤,标点符号已被删除。
是否可以取出标点符号并将其插入已翻译的段落中?
答案 0 :(得分:0)
制作两个数组。一个用于英语,一个用于另一种语言。您可以从数据库中动态填充它们。
然后使用str_ireplace功能。
$english = array("potato", "however");
$lang = array("budfun", "ejijzow");
$words = "However, the local importance of the potato is variable and changing rapidly. It remains an essential crop in Europe (especially eastern and central Europe), where per capita production is still the highest in the world, but the most rapid expansion over the past few decades has occurred in southern and eastern Asia. As of 2007 China led the world in potato production, and nearly a third of the world's potatoes were harvested in China and India.";
$translation = str_ireplace($english, $lang, $words);
echo $translation;