I'm kinda stuck with the following:

I have on my form a regular text field where the user is supposed to enter values (numbers) separated by spaces.

When processing the form, I try to both extract the values and count the number of values user has given, like this:

$numberofvalues=sizeof(explode(" ", trim($inputfield)));

Everything works OK, if the user input is in correct format (e.g. "1 2 3"), and the trim() takes care of leading and trailing spaces, but the problems arise if the user input contains more than one spaces between the values, like "1 2 3".
All of a sudden the array generated by explode() has 4 elements instead of 3.

How can I circumvent this?

--janne

    Well, one solution would be;


    $new = array();
    foreach (explode(' ', $inputfield) AS $key => $val) {
    if ($val != '') $new[] = $val;

    }

    This would place the values from $inputfield into a new array, $new. Hope it helps.

      Another would be

      $numberofvalues=sizeof(preg_split("/[ ]+/", trim($inputfield)));

      The [] are optional - they're there only so you can see the space 🙂

        <?php
        $sqlins = "select craftentry2002.cr_total, programentry2002.pr_total from (craftentry2002 INNER JOIN programentry2002 on programentry2002.leoid = craftentry2002.leoid) where leoid='1P16ZZGK'";
        $res = mysql_db_query ($db, $sqlins, $link);
        $row4 = mysql_fetch_array($res);
        mysql_db_query ($db, $sqlins, $link);
        echo "$row4[programentry2002.pr_total]\n";
        ?>

          <?php
          $sqlins = "select craftentry2002.cr_total, programentry2002.pr_total from (craftentry2002 LEFT JOIN programentry2002 on programentry2002.leoid = craftentry2002.leoid) where craftentry2002.leoid='1P16ZZGK'";
          $res = mysql_db_query ($db, $sqlins, $link);
          $row4 = mysql_fetch_array($res);
          mysql_db_query ($db, $sqlins, $link) or die ("failed: " . mysql_error().' sql: '.$sqlins);
          echo "$row4[cr_total] - $row4[pr_total]\n";
          ?>

            <?php
            $sqlins = "select craftentry2002.cr_total, programentry2002.pr_total from (craftentry2002 LEFT JOIN programentry2002 on programentry2002.leoid = craftentry2002.leoid) where craftentry2002.leoid='1P16ZZGK'";
            $res = mysql_db_query ($db, $sqlins, $link) or die ("failed: " . mysql_error().' sql: '.$sqlins);
            $tabulate = ($row4[cr_total] + $row4[pr_total]);
            while ($row4 = mysql_fetch_array ($res)) {
            echo "$tabulate\n";
            }
            ?>

              <?php
              $sqlins = "select SUM(craftentry2002.cr_total), SUM(programentry2002.pr_total) from (craftentry2002 LEFT JOIN programentry2002 on programentry2002.leoid = craftentry2002.leoid) where craftentry2002.leoid='1P16ZZGK'";
              $res = mysql_db_query ($db, $sqlins, $link) or die ("failed: " . mysql_error().' sql: '.$sqlins);
              $row4 = mysql_fetch_array($res);
              ?>

                Write a Reply...