【问题标题】:Explode string to array PHP将字符串分解为数组 PHP
【发布时间】:2016-11-07 17:47:14
【问题描述】:

我有两个 PHP 表单的输入字段。这些字段需要保存到数组中,并且输出应该在括号中包含每个单词的单词长度。还需要评估输入字段 1 或 2 的单词最长,以及每个字段中有多少单词。它在执行过程中显示错误。语法错误,文件意外结束 Screenshot 1Screenshot 2

<!DOCTYPE html ">
<html>
    <head>
        <meta charset="UTF-8">
            <title> Two Input field compare PHP </title>
    </head>
    <body> 
        <?php

        echo "<br/>";

        if (isset($_POST['submit'])) {
            $input1 = $_POST['input1'];
            $input2 = $_POST['input2'];
            //example $input1 = "Learning the PHP";
           //example $input2 = "Counting words and showing(7) like this word<< ";

            if (empty($input1)) {
                echo "You need to enter both fields !";
            }
            if (empty($input2)) {
                echo "You need to enter both fields !";
            }

              echo $input1;
              echo $input2;


            $inputarr1 = explode(" ", $input1);
            $inputarr2 = explode(" ", $input2);



            $longestlenghtarr1 = $longestlenghtarr2 = 0;

            $arraylength1 = sizeof($inputarr1);
            $arraylength2 = sizeof($inputarr2);



            $longest1 = $longest2 = 0;

            for ($x = 0; $x < arraylength1; $x++) {

            echo $inputarr1[$x] . " &lt; " . strlen($inputarr1[$x]) . " &gt; ";
                if (strlen($inputarr1[$x]) > $longest1) {
                    $longest1 = strlen($inputarr1[$x]);
                }
            }

            for ($y = 0; $y < arraylength2; $y++) {
                echo $inputarr2[$y] . " &lt; " . strlen($inputarr2[$y]) . " &gt; ";

                if (strlen($inputarr2[$y]) > $longest2) {
                    $longest2 = strlen($inputarr2[$y]);
                }
            }

                if ($longest1 > $longest2) {
                echo "<br/> The field 1 input has longest word of lenght " . $longest1." characters !";
                }

                if ($longest2 > $longest1) {
                        echo "<br/> The field 2 input has longest word of lenght " .$longest2." characters !";
                    }

                    if ($arraylength1 > $arraylength2) {
                    echo "<br/> The field 1 input has more words";}
                        if ($arraylength2 > $arraylength1) {
                            echo "<br/> The field 2 input has more words";
                        }

                        echo "<br/>";

              ?>

             <div>
                            <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

                Input Text 1 <br/>
                <input type="text" name="input1" /><br/>
                Input Text 2 <br/>
                <input type="text" name="input2" /><br/>
                <input type="submit" name="submit" value="Submit" />
            </form>

        </div>

 </body></html>

【问题讨论】:

  • 你能再帮忙吗!
  • 你真的只需要调试它。通常,当您遇到错误时,您要做的第一件事就是启用错误报告。但是由于某种原因,您已经竭尽全力禁用它:error_reporting(0);。 “它在执行过程中显示错误。”什么错误? 您需要输入两个字段!
  • 我修改了代码。可以再测试一下吗?

标签: php arrays input field explode


【解决方案1】:

可能是错误的变量名 - 它应该会导致类似 Undefined index 'input1' 的错误。

尝试将$POST_ 替换为$_POST,这样您就应该拥有

$input1 = $_POST['input1']; 
$input2 = $_POST['input2'];

【讨论】:

  • 仍然没有输出。需要验证字符串爆炸和计数
  • 首先@HPierce sais 尝试将error_reporting(0); 替换为error_reporting(E_ALL);,或者只是注释该行以查看代码中的错误。您的 explode 函数的第一个参数必须是 char (您有空字符串),所以在问号之间放一个空格。而不是你有$inputarr1 = explode...,但后来你正在迭代$inputarray1(不同的数组)。
猜你喜欢
  • 1970-01-01
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2018-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多