dt1888 2013-04-26 00:35
浏览 61
已采纳

嵌套条件使用单选按钮引导PHP中的脚本

I've been working on this for a while and I can't seem to figure it out. I know it must be something really simple. Basically I have a script that works as a program that translates English to Piglatin, and it works fine, but I want the user to have a choice of whether or not to actually operate that script, by using a radio form with the text input that says "English" or "Piglatin". I've tried all different ways to get this to work, but using a nested conditional seems like it would be the most logical answer to me. However, whenever I try to run the script with it, it doesn't work. Can someone please tell me what I'm doing wrong?! It would be much appreciated. Thanks!

HTML Form:

<p><input type="text" name="original" size="20" maxlength="40" /></label></p>
<p><input type="radio" name="english" value="yes"/>english <input type="radio" name="english" value="no"/>piglatin</p>
<input type="submit" name="submit" value="submit" /></form>

PHP:

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

if($english=="no")
{
piglatin = "";
foreach($array as $word)
{
    $word = trim($word);
    $first = substr($word,0,1);
    $rest = substr($word,1,strlen($word)-1);

if (preg_match('/^[aeiou]/', $word)) {
    $word = preg_replace('/^([aeiou].+)$/', "$1-way", $word);
}

elseif (preg_match('/^(th|sh)/', $word)) {
    $word = preg_replace('/^(th|sh)(.+)$/', "$2-$1ay", $word);
}

    else {
       $word = preg_replace('/^[a-z](.+)$/', "$1-$first"."ay", $word);
    }

    $piglatin .= $word ." ";


echo $original ." becomes: ".$piglatin.".";
};

else
{echo $original.".";
};


?>

Like I said, I'm sure it's something really small and simple that I just can't see because I've been looking at the code so long. Any help is appreciated! Thank you!

  • 写回答

1条回答 默认 最新

  • doww38701 2013-04-26 00:52
    关注

    Sort your indentation out and you will see your missing closing brackets.

    <?php # script
      $original = $_REQUEST['original'];
      $english = $_REQUEST['english'];
      $array = explode(" ", $original);
    
      if($english=="no")
      {
        $piglatin = "";
        foreach($array as $word)
        {
          $word = trim($word);
          $first = substr($word,0,1);
          $rest = substr($word,1,strlen($word)-1);
    
          if (preg_match('/^[aeiou]/', $word)) {
              $word = preg_replace('/^([aeiou].+)$/', "$1-way", $word);
          } elseif (preg_match('/^(th|sh)/', $word)) {
              $word = preg_replace('/^(th|sh)(.+)$/', "$2-$1ay", $word);
          } else {
              $word = preg_replace('/^[a-z](.+)$/', "$1-$first"."ay", $word);
          }
          $piglatin .= $word ." ";
          echo $original ." becomes: ".$piglatin.".";
        };
      } else {
        echo $original.".";
      };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看