检测和替换字符串中最后一个元音的最有效方法是什么?


What is the most efficient way to detect and replace the last vowel in a string?

我有一个字符串,其中包含一系列以逗号分隔的随机单词:

worda,sample,wordb,another,extra,exampleb

这个列表总是不同的。

用给定的$变量替换每个单词的最后一个元音的最有效方法是什么?

$vowel = array('a','e','i','o','u');
    $vowel = join('',$vowel);
    $str = "worda,sample,wordb,another,extra,exampleb";    
    $str = preg_replace("([{$str}]?(,|$))U","___$1",$str);     

这可能会给你一些想法…

    $replacement = '*';
    $string = 'super,cali,fragil,istic,expi,alido,cious';
    echo $string.'<br />';
    $vowels = array('a', 'e', 'i', 'o', 'u');
    $words = explode(',', $string);
    foreach($words as $word_key => $word)
    {
        $word = strrev($word);
        $chars = str_split($word);
        foreach($chars as $char_key => $char)
        {
            if(in_array($char, $vowels))
            {
                $word[$char_key] = $replacement;
                $words[$word_key] = strrev($word);
                continue(2);
            }
        }
    }
    $new_str = implode(',', $words);
    echo $new_str;die;

修改为使用isset()代替in_array()…

    $replacement = '*';
    $string = 'super,cali,fragil,istic,expi,alido,cious';
    echo $string.'<br />';
    $vowels = array(
        'a' => 1,
        'e' => 1,
        'i' => 1,
        'o' => 1,
        'u' => 1
    );
    $words = explode(',', $string);
    foreach($words as $word_key => $word)
    {
        $word = strrev($word);
        $chars = str_split($word);
        foreach($chars as $char_key => $char)
        {
            if(isset($vowels[$char]))
            {
                $word[$char_key] = $replacement;
                $words[$word_key] = strrev($word);
                continue(2);
            }
        }
    }
    $new_str = implode(',', $words);
    echo $new_str;die;

简单,更有效的方法…正则表达式。它花了一点时间来玩,但它可以满足你的需要,如果我理解正确的话。

<?php
$variable = "_";
$regexp = "/([aeiou]+)([^aeiou]*)(',|$)/";
$string = "worda,sample,wordb,another,extra,exampleb";
$new_string = preg_replace($regexp, $variable."$2,", $string);
echo $new_string;   
?>

输出是:word_、sampl_ w_rdb, anoth_r、extr_ exampl_b,

从字符串的末尾开始搜索。遇到的第一个元音替换为$variable。然后继续,用$variable替换每个逗号后面遇到的第一个元音。

在PHP中应该是这样的:

function is_vowel($c)
{
  return $c == 'A' || $c == 'E' || $c == 'I' || $c == 'O' || $c == 'U' ||
         $c == 'a' || $c == 'e' || $c == 'i' || $c == 'o' || $c == 'u';
}
$s = 'some string';
$variable = 'replacement string';
$len = strlen($s);
$i = $len - 1;
$must_replace = true;
$result = '';
while ($i >= 0)
{
  $c = substr($s, $i, 1);
  if ($must_replace && is_vowel($c))
  {
    $must_replace = false;
    $result = $variable . $result;
  }
  else
  {
    $result = $c . $result;
    if ($c == ',')
    {
      $must_replace = true;
    }
  }
  $i = $i - 1;
}

也许有些步骤可以使用PHP内置函数更有效地执行,但这段代码应该给出了所建议算法的思想。

<?php
$string = 'worda,sample,wordb,another,extra,exampleb,RFL';
$variable = 'replaced';
$vowels = array('a','e','i','o','u');
$words = explode(',',$string);
// for each word..
foreach($words as $index => $word)
{
    //this will be our new word, if there is a vowel found
    $new_word = '';
    for($char_index = strlen($word) - 1; $char_index > 0; $char_index--)
    {
        // the character we've selected; remember: a string is an array of characters
        $selected_char = $word[$char_index];
        // is the selected character in the vowels?
        if(in_array($selected_char,$vowels))
        {
            // find the last occurrence of the vowel we found
            $position = strrpos($word,$selected_char);
            // substr the first N characters before the vowel we found,
            // then add on the replacement and the last characters
            $new_word = substr($word,0,$position).str_replace($selected_char,$variable,substr($word,$position,strlen($word)));
            break;
        }
    }
    // since new_word will be string length > 0 for modifications, we replace it
    if(strlen($new_word) > 0)
    {
        $words[$index] = $new_word;
    }
}
?>