dqvj51875 2013-04-25 00:24
浏览 38

没有正则表达式的Piglatin程序的PHP脚本 - 如何考虑以元音,“th”或“sh”开头的单词?

Basically I'm trying to write a pretty basic program in PHP that just takes user input and translates it into Piglatin with PHP without using regular expressions. This is what my code so far looks like, which is fine:

<?php # script
$original = $_REQUEST['original'];
$array = explode(" ", $original);

$piglatin = "";
foreach($array as $word)
{
    $word = trim($word);
    $first = substr($word,0,1);
    $thsh = substr($word,1,2);
    $thshrest = substr($word,2, strlen($word)-2);
    $rest = substr($word,1,strlen($word)-1);

    if(trim($word)) 
    {
        $piglatin .= (strlen($word)==1)?$first." ":$rest.$first. "ay ";
    }
}

echo $original ." becomes: ".$piglatin;
?>

except it doesn't take into account the special cases, like if a word begins with a vowel (in which case, the word "igloo" for example should be printed as "iglooway"), or if it begins with "th" or "sh" (in which case, the word "thimble" for example should be printed as "imblethay", taking the first two letters and bringing them to the end instead of just the first one.)

I've already started the process of creating variables out of the strings that start with "th" and "sh" (see $thsh and $thshrest), but I'm really confused as to where I should go from here?

All help is appreciated, thank you!

  • 写回答

1条回答 默认 最新

  • douhui4699 2014-01-30 17:46
    关注

    Yeah, without using regex you'll need to manually check for all cases:

    if ($first == "a" || $first == "e" || $first == "i" || $first == "o" || $first == "u"){
      $piglatin .= (strlen($word)==1)?$first." ":$word. "ay ";
    } elseif ($thsh == "th" || $thsh == "sh") {
      $piglatin .= (strlen($word)==1)?$first." ":$thshrest.$thsh. "ay ";
    } else {
      $piglatin .= (strlen($word)==1)?$first." ":$rest.$first. "ay ";
    }
    

    (I haven't tested this code)

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度