【问题标题】:How to split Tamil characters in a string in PHP如何在 PHP 中拆分字符串中的泰米尔语字符
【发布时间】:2012-02-06 13:37:53
【问题描述】:

如何在字符串中拆分泰米尔语字符?

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

如何获得组合字符“த”、“மி”和“ழ்”?

【问题讨论】:

    标签: php unicode string-split tamil


    【解决方案1】:

    如果我正确理解您的问题,您有一个包含代码点的 unicode 字符串,并且您想将其转换为一个图形数组?

    我正在开发一个开源 Python 库来为 Tamil Language website 执行此类任务。

    我有一段时间没有使用 PHP,所以我将发布逻辑。你可以看看amuthaa/TamilWord.py file's split_letters() function中的代码。

    正如 ruakh 所提到的,泰米尔语字素被构建为代码点。

    • 元音(உயிர்எழுத்து),Aytham(ஆய்தஎழுத்து - ஃ)和“A”柱中的所有组合((உயிர்-மெய்எழுத்து)(அவரி - 即,ச,ட,த, ப、ற、ங、ஞ、ண、ந、ம、ன、ய、ர、ள、வ、ழ、ல)每个都使用一个代码点。

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

    • 除 a 组合之外的每个组合也由两个代码点组成: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
    

    【讨论】:

      【解决方案2】:

      我认为您应该能够使用the grapheme_extract function 来迭代组合字符(技术上称为“字素簇”)。

      或者,如果您更喜欢正则表达式方法,我认为您可以使用此方法:

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

      其中\pL 表示Unicode“字母”,\pM 表示Unicode“标记”。

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

      【讨论】:

      • accept this answer if it helped you solve your problem。这将清楚地表明该问题已得到令人满意的回答,为未来的访问者提供有关哪个答案有效(如果有)的指标,并为获得答案的人提供一点额外的声誉,以激励他们继续回答问题。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多