如何在PHP中分割一个字符串中的泰米尔字符

10

我该如何在字符串中分割泰米尔字符?

当我使用preg_match_all('/./u', $str, $results)时,我得到了字符“த”,“ம”,“ி”,“ழ”和“்”。

我该如何获取组合字符“த”,“மி”和“ழ்”?

2个回答

14

我认为你可以使用grapheme_extract函数来遍历组合字符(在技术上称为“图形簇”)。

另外,如果你更喜欢正则表达式的方法,我认为你可以使用以下代码:

preg_match_all('/\pL\pM*|./u', $str, $results)

\pL 表示 Unicode "字母",\pM 表示 Unicode "标记"。

(免责声明:我没有测试过这些方法。)


7
请在此答案帮助您解决问题后接受此答案。这将清楚地表明问题已得到令人满意的回答,并为未来的访问者提供一个指示,说明哪个答案起作用(如果有),并为回答者提供额外的声誉作为继续回答问题的激励。谢谢! - sarnold

3
如果我理解你的问题正确,你有一个包含码点的Unicode字符串,想要将其转换为一个图形符号数组?
我正在开发一个开源的Python库来处理这样的任务,为泰米尔语网站提供支持。
我已经有一段时间没有使用PHP了,所以我会发布逻辑。您可以查看amuthaa/TamilWord.py文件的split_letters()函数中的代码。
正如ruakh所提到的,泰米尔语的图形符号是由码点构成的。
  • 元音字母(உயிர் எழுத்து)、aytham(ஆய்த எழுத்து - ஃ)以及所有组合((உயிர்-மெய் எழுத்து)在'a'列中(அ வரி - 即 க、ச、ட、த、ப、ற、ங、ஞ、ண、ந、ம、ன、ய、ர、ள、வ、ழ、ல)每个使用单个码位。

  • 每个辅音都由两个码位组成:a组合字母+ pulli。例如:ப் = ப + ்

  • 除了a组合之外的每个组合也由两个码位组成:a组合字母+标记:例如:பி = ப் + ி,தை = த் + ை

因此,如果您的逻辑是这样的:

initialize an empty array

for each codepoint in word:

    if the codepoint is a vowel, a-combination or aytham, it is also its grapheme, so add it to the array

    otherwise, the codepoint is a marking such as the pulli (i.e. ்) or one of the combination extensions (e.g.  ி or  ை), so append it to the end of the last element of the array

当然,这假设你的字符串格式良好,没有两个标记连续出现的情况。
以下是Python代码,如果您觉得有帮助。如果您想帮助我们将其移植到PHP,请告诉我:
@staticmethod
def split_letters(word=u''):
    """ Returns the graphemes (i.e. the Tamil characters) in a given word as a list """

    # ensure that the word is a valid word
    TamilWord.validate(word)

    # list (which will be returned to user)
    letters = []

    # a tuple of all combination endings and of all அ combinations
    combination_endings = TamilLetter.get_combination_endings()
    a_combinations = TamilLetter.get_combination_column(u'அ').values()

    # loop through each codepoint in the input string
    for codepoint in word:

        # if codepoint is an அ combination, a vowel, aytham or a space,
        # add it to the list
        if codepoint in a_combinations or \
            TamilLetter.is_whitespace(codepoint) or \
            TamilLetter.is_vowel(codepoint) or \
            TamilLetter.is_aytham(codepoint):

            letters.append(codepoint)

        # if codepoint is a combination ending or a pulli ('்'), add it
        # to the end of the previously-added codepoint
        elif codepoint in combination_endings or \
            codepoint == TamilLetter.get_pulli():

            # ensure that at least one character already exists
            if len(letters) > 0:
                letters[-1] = letters[-1] + codepoint

            # otherwise raise an Error. However, validate_word()
            # should catch this
            else:
                raise ValueError("""%s cannot be first character of a word""" % (codepoint))

    return letters

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接