PHP 数组还是 MySQL 翻译?怎样做?

发布于 2024-12-01 18:30:02 字数 260 浏览 0 评论 0原文

我想创建一个仅包含文本区域和输出文本占位符的页面。用户将文本输入到文本区域,当他们键入时(或按下输入按钮时),文本将被翻译(或更重要的是 - 被交换)。例如,我可能会写一些类似“以我诚实的观点”或“说实话”之类的内容,这会将文本转换为其缩写形式,恕我直言,TBH,我考虑过用数组来做到这一点。可以首先搜索文本中的较长字符串,即我想到有一个由四个单词短语、三个单词短语、两个单词短语和一个可以缩写的单词组成的数组,它们可以在“key”=> ; “值”形式。这会工作吗,或者 mysql 会工作得更好吗?

I would like to create a page that contains just a textarea and placeholder for the output text. The user would enter text into the textarea and while they type (or when the enter button is pressed) the text is translated (or more to the point - swapped out). For example, I might write something like "In my honest opinion", or "to be honest" and that would convert the text to its abbreviated form IMHO and TBH, I thought about doing this with arrays. The text could be searched for the longer strings first, i.e. I thought about have an array of four word phrases, three word phrases, two word ones and a single array of words that can be abbreviated, they could be in "key" => "value" form. Would this work, or would mysql work better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

又爬满兰若 2024-12-08 18:30:02

听起来您正在寻求将全文映射为缩写文本。这应该像实际的 i18n 翻译一样工作,因此任何翻译实现都适合。

即使您不使用 Zend Framework,您也可以查看 Zend_Translate 适配器 查看其支持的适合候选者的映射解决方案。

实际上,除了告诉您映射 key => 之外, value 绝对是正确的选择;如何存储地图完全取决于您。

更新

$map = array(
    'in my honest opinion' => 'IMHO',
    'to be honest' => 'TBH'
);

// Really simple search/replace
$translated = str_replace(array_keys($map), $map, $string_to_translate);

这显示了一个非常简单的搜索/替换,但是替换的复杂性将取决于输入的类型和您期望的替换类型

It sounds like you're looking to map full text into abbreviated text. This should work just like actual i18n translation, so any translation implementation would suit.

Even if you don't use Zend Framework, you could look at Zend_Translate adapters to see their supported mapping solutions for a suitable candidate.

Really though, beyond telling you that mapping that key => value is definitely the way to go; how you store the map is entirely up to you.

Updated

$map = array(
    'in my honest opinion' => 'IMHO',
    'to be honest' => 'TBH'
);

// Really simple search/replace
$translated = str_replace(array_keys($map), $map, $string_to_translate);

This shows a really simple search/replace, but the complexity of your replacing will depend on the type of input and the type of replacement you expect

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文