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

时间:2011-07-26 05:09:23

标签: php string

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

worda,sample,wordb,another,extra,exampleb

此列表将始终不同。

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

5 个答案:

答案 0 :(得分:3)

$vowel = array('a','e','i','o','u');
    $vowel = join('',$vowel);
    $str = "worda,sample,wordb,another,extra,exampleb";    

    $str = preg_replace("([{$str}]?(,|$))U","___$1",$str);     

答案 1 :(得分:2)

这可能会给你一些想法......

    $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;

答案 2 :(得分:2)

简单,高效的方式......正则表达式。如果我理解你需要的话,它需要一点点玩,但它可以满足您的需求。

<?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,

答案 3 :(得分:1)

从字符串的末尾开始搜索。您遇到的第一个元音用$ 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内置函数可以更有效地执行某些步骤,但是这段代码应该给出了所提议算法的想法。

答案 4 :(得分:1)

<?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;
    }
}
?>