php - 将多个正则表达式合并为一个

标签 php regex string

我正在尝试编写代码以将字符串连字成拉丁文。我已经解决了一些限制,但是我没有得到想要的输出。我的代码如下:

<?php

$string = "impulerittantaenanimis caelestibusirae";

$precedingC = precedingConsonant($string);
$xrule = xRule($precedingC);
$consonantc = consonantCT($xrule);
$consonantp = consonantPT($consonantc);
$cbv = CbetweenVowels($consonantp);
$tv = twoVowels($cbv);

echo $tv;

function twoVowels($string)
{
    return preg_replace('/([aeiou])([aeiou])/', '$1-$2', $string);
}
function CbetweenVowels($string)
{
    return preg_replace('/([aeiou])([^aeiou])([aeiou])/', '$1-$2$3', $string);
}
function consonantPT($string)
{
    return preg_replace('/([^aeiou]p)(t[aeiou])/', '$1-$2', $string);
}
function consonantCT($string)
{
    return preg_replace('/([^aeiou]c)(t[aeiou])/', '$1-$2', $string);
}
function precedingConsonant($string)
{
    $arr1 = str_split($string);
    $length = count($arr1);
    for($j=0;$j<$length;$j++)
    {
        if(isVowel($arr1[$j]) && !isVowel($arr1[$j+1]) && !isVowel($arr1[$j+2]) && isVowel($arr1[$j+3]))
        {
            $pc++;  
        }
    }

    function strAppend2($string)
    {
        $arr1 = str_split($string);
        $length = count($arr1);


        for($i=0;$i<$length;$i++)
        {
            $check = $arr1[$i+1].$arr1[$i+2];
            $check2 = $arr1[$i+1].$arr1[$i+2].$arr1[$i+3];
            if($check=='br' || $check=='cr' || $check=='dr' || $check=='fr' || $check=='gr' || $check=='pr' || $check=='tr' || $check=='bl' || $check=='cl' || $check=='fl' || $check=='gl' || $check=='pl' || $check=='ch' || $check=='ph' || $check=='th' || $check=='qu' || $check2=='phl' || $check2=='phr')
            {
                if(isVowel($arr1[$i]) && !isVowel($arr1[$i+1]) && !isVowel($arr1[$i+2]) && isVowel($arr1[$i+3]))
                {
                    $updatedString = substr_replace($string, "-", $i+1, 0);
                    return $updatedString;
                }
            }
            else
            {
                if(isVowel($arr1[$i]) && !isVowel($arr1[$i+1]) && !isVowel($arr1[$i+2]) && isVowel($arr1[$i+3]))
                {
                    $updatedString = substr_replace($string, "-", $i+2, 0);
                    return $updatedString;
                }
            }
        }
    }
    $st1 = $string;
    for($k=0;$k<$pc;$k++)
    {
        $st1 = strAppend2($st1);
    }

    return $st1;
}
function xRule($string)
{
    return preg_replace('/([aeiou]x)([aeiou])/', '$1-$2', $string);
}
function isVowel($ch)
{
    if($ch=='a' || $ch=='e' || $ch=='i' || $ch=='o' || $ch=='u')
    {
        return true;
    }
    else
    {
        return false;
    }
}
function isConsonant($ch)
{
    if($ch=='a' || $ch=='e' || $ch=='i' || $ch=='o' || $ch=='u')
    {
        return false;
    }
    else
    {
        return true;
    }
}

?>

我相信,如果我将所有这些功能结合起来,就会产生所需的输出。但是,我将在下面指定我的限制条件:

Rule 1 : When two or more consonants are between vowels, the first consonant is joined to the preceding vowel; for example - rec-tor, trac-tor, ac-tor, delec-tus, dic-tator, defec-tus, vic-tima, Oc-tober, fac-tum, pac-tus, 

Rule 2 : 'x' is joined to the preceding vowel; as, rex-i. 

However we give a special exception to the following consonants - br, cr, dr, fr, gr, pr, tr; bl, cl, fl, gl, pl, phl, phr, ch, ph, th, qu. These consonants are taken care by adding them to the later vowel for example - con- sola-trix
n- sola-trix. 

Rule 3 : When 'ct' follows a consonant, that consonant and 'c' are both joined to the first vowel for example - sanc-tus and junc-tum

Similarly for 'pt' we apply the same rule for example - scalp-tum, serp-tum, Redemp-tor. 

Rule 4 : A single consonant between two vowels is joined to the following vowel for example - ma-ter, pa-ter AND Z is joined to the following vowel. 

Rule 5 : When two vowels come together they are divided, if they be not a diphthong; as au-re-us. Diaphthongs are - "ae","oe","au"

最佳答案

如果仔细查看每条规则,您会发现所有规则都涉及开头的元音或前面的元音。一旦你意识到这一点,你就可以尝试构建一个单一的模式,将 [aeiou] 放在开始的因素中:

$pattern = '~
    (?<=[aeiou]) # each rule involves a vowel at the beginning (also called a
                 # "preceding vowel")
    (?:
        # Rule 2: capture particular cases
        ( (?:[bcdfgpt]r | [bcfgp] l | ph [lr] | [cpt] h | qu ) [aeiou] x )
      |
        [bcdfghlmnp-tx]
        (?:
            # Rule 3: When "ct" follows a consonant, that consonant and "c" are both
            # joined to the first vowel
            [cp] \K (?=t)
          |
            # Rule 1: When two or more consonants are between vowels, the first
            # consonant is joined to the preceding vowel
            \K (?= [bcdfghlmnp-tx]+ [aeiou] )
        )   
      |
        # Rule 4: a single consonant between two vowels is joined to the following
        # vowel
        (?:
            \K (?= [bcdfghlmnp-t] [aeiou] )
          | 
            # Rule 2: "x" is joined to the preceding vowel
            x \K (?= [a-z] | (*SKIP)(*F) ) 
        )
      |
        # Rule 5: When two vowels come together they are divided, if they not be a
        # diphthong ("ae", "oe", "au")
        \K (?= [aeiou] (?<! a[eu] | oe ) )
    )
~xi';

此模式旨在仅匹配放置连字符的位置(规则 2 的特殊情况除外),这就是为什么它使用大量 \K 来开始匹配结果的原因position 和 lookaheads 来测试后面没有匹配字符的内容。

$string = <<<EOD
Aeneadum genetrix, hominum diuomque uoluptas,
alma Uenus, caeli subter labentia signa
quae mare nauigerum, quae terras frugiferentis
concelebras, per te quoniam genus omne animantum
EOD;

$result = preg_replace($pattern, '-$1', $string);

Ae-ne-a-dum ge-ne-trix, ho-mi-num di-u-om-qu-e u-o-lup-tas,
al-ma U-e-nus, cae-li sub-ter la-ben-ti-a sig-na
qu-ae ma-re nau-i-ge-rum, qu-ae ter-ras fru-gi-fe-ren-tis
con-ce-leb-ras, per te qu-o-ni-am ge-nus om-ne a-ni-man-tum

请注意,我没有包括几个拉丁字母表中不存在的字母,如 k、y 和 z,如果您需要处理翻译的希腊语单词或其他单词,请随意包括它们。

关于php - 将多个正则表达式合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40006835/

相关文章:

php - 用于测试目的的对本地主机的简单 session 固定攻击

python - 为什么下划线不能匹配 '\W' ?

javascript - 简单 `replace()` 的第二个参数的问题

java - 这个 Java 代码片段是如何工作的? (字符串池和反射)

java - 具有多个分隔符的 Split()(不起作用)

php - MySQL PHP - 如何显示存储在数据库中的不可读字符

PHP MySQL 选择一个 SUM 减去一个 SUM

php - 查看日期/时间字符串是否包含时区

c - 在 C 中使用 scanf 的正则表达式

Javascript 为什么返回 NaN?