Correct string-split and reassemble?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert

    Correct string-split and reassemble?

    Hello,

    Can anyone help with this code?

    I need to split a long piece of text from a textarea box into small
    chunks, then POST these chunks to my credit-card provider, whereupon he
    will POST them back to my script, which will reassemble them into the
    original text and display it. This is necessary because my credit-card
    provider has a limit on all POSTed variables, which must be 255
    characters or less each.

    I thought that this would be simple to do. I used substr to take bites
    out of the string, then included each piece as a hidden variable in my
    form. When I received the return POST, I reassembled all the strings
    using the (.) concatenator, then printed this out.

    The problem is, pieces of the original text are missing, and there are
    numerous // escape elements present in the text.

    Here is my code:

    $questions = $_POST['questions']; // coming from a previous POST

    $questions=subs tr($questions, 0, 3556); // I put a limit on the total
    string size.

    $questions0=sub str($questions, 0, 254);
    $questions1=sub str($questions, 254, 254);
    $questions2=sub str($questions, 508, 254);
    $questions3=sub str($questions, 762, 254); // etc.

    <INPUT TYPE="HIDDEN" NAME="questions 0" value="<?=$ques tions0?>" >
    <INPUT TYPE="HIDDEN" NAME="questions 1" value="<?=$ques tions1?>" >
    <INPUT TYPE="HIDDEN" NAME="questions 2" value="<?=$ques tions2?>" >
    <INPUT TYPE="HIDDEN" NAME="questions 3" value="<?=$ques tions3?>" > //
    etc.

    Then I POST the data, and then it comes back to my redirect page, which
    uses the following code:

    $questions0=$_P OST['questions0'];
    $questions1=$_P OST['questions1'];
    $questions2=$_P OST['questions2'];
    $questions3=$_P OST['questions3']; // etc.

    $questions=$que stions0 . $questions1 . $questions2 . $questions3 .
    $questions4 . $questions5 . $questions6 . $questions7 . $questions8 .
    $questions9 . $questions10 . $questions11 . $questions12 . $questions13
    .. $questions14; // concatenate it

    print ($questions); // I display $questions. It is mostly there, with
    parts missing, and many //s here and there.

    Any ideas?

    Thank you.

  • badr

    #2
    Re: Correct string-split and reassemble?

    hi , iam not sure of your algorithm , but i need to see the valid form
    of the credit-card that can be accepted by your provider ,
    now i may hint somthing
    while you are using textarea i think you have to be sure of the spaces:
    1-trim the input form both sides (left,rgiht) using
    trim($_POST['your_quiz']).
    2-make sure there is no inner spaces in the input using
    strpos($your_va r), so if you have any spaces in the input remove it
    before any other process.
    -----------------------------------------
    but you may can concatenate your text using implode(".",$va raible)
    if your still in problem let me know we may also use REGEXP to check
    the data, see u.

    Comment

    • Pedro Graca

      #3
      Re: Correct string-split and reassemble?

      Robert wrote:[color=blue]
      > The problem is, pieces of the original text are missing, and there are
      > numerous // escape elements present in the text.[/color]

      Do you mean extra \ (backslashes)?
      I suspect you (or the credit-card provider) are running with
      "magic_qutes_gp c" set.

      Either turn them off or stripslashes() from the input.






      A few comments about your code (snippets not tested):
      [color=blue]
      > $questions0=sub str($questions, 0, 254);
      > $questions1=sub str($questions, 254, 254);
      > $questions2=sub str($questions, 508, 254);
      > $questions3=sub str($questions, 762, 254); // etc.[/color]

      Ugh!

      define('CHUNK_S IZE', 254);
      $chunks = 1 + strlen($questio ns)/CHUNK_SIZE;
      for ($i = 0; $i < $chunks; ++$i) {
      /* using variable variables */
      $q = 'questions' . $i;
      $$q = substr($questio ns, CHUNK_SIZE * $i, CHUNK_SIZE);
      }


      IMO, even better would be to use arrays:

      define('CHUNK_S IZE', 254);
      $qs = array();
      $chunks = 1 + strlen($questio ns)/CHUNK_SIZE;
      for ($i = 0; $i < $chunks; ++$i) {
      $qs[] = substr($questio ns, CHUNK_SIZE * $i, CHUNK_SIZE);
      }
      [color=blue]
      > <INPUT TYPE="HIDDEN" NAME="questions 0" value="<?=$ques tions0?>" >
      > <INPUT TYPE="HIDDEN" NAME="questions 1" value="<?=$ques tions1?>" >
      > <INPUT TYPE="HIDDEN" NAME="questions 2" value="<?=$ques tions2?>" >
      > <INPUT TYPE="HIDDEN" NAME="questions 3" value="<?=$ques tions3?>" > //
      > etc.[/color]

      Using the $qs array:

      $n = 0; /* probably not needed, see ## below */
      foreach ($qs as $q) {
      /* when the for with these inputs is submitted */
      /* $_POST['questions'] will be an array */
      echo '<input type="hidden" name="questions[', $n++, ']" value="', $q, '"/>';

      /* if you want to try it without the $n */
      ## echo '<input type="hidden" name="questions[]" value="', $q, '"/>';
      }
      [color=blue]
      > Then I POST the data, and then it comes back to my redirect page, which
      > uses the following code:
      >
      > $questions=$que stions0 . $questions1 . $questions2 . $questions3 .
      > $questions4 . $questions5 . $questions6 . $questions7 . $questions8 .
      > $questions9 . $questions10 . $questions11 . $questions12 . $questions13
      > . $questions14; // concatenate it[/color]

      $questions = implode('', $_POST['questions']);
      [color=blue]
      > print ($questions); // I display $questions. It is mostly there, with
      > parts missing, and many //s here and there.[/color]


      Happy Coding :-)

      --
      Mail to my "From:" address is readable by all at http://www.dodgeit.com/
      == ** ## !! ------------------------------------------------ !! ## ** ==
      TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
      may bypass my spam filter. If it does, I may reply from another address!

      Comment

      • David Gillen

        #4
        Re: Correct string-split and reassemble?

        An noise sounding like Robert said:[color=blue]
        > Hello,
        >
        > Can anyone help with this code?
        >
        > I need to split a long piece of text from a textarea box into small
        > chunks, then POST these chunks to my credit-card provider, whereupon he
        > will POST them back to my script, which will reassemble them into the
        > original text and display it. This is necessary because my credit-card
        > provider has a limit on all POSTed variables, which must be 255
        > characters or less each.
        >
        > I thought that this would be simple to do. I used substr to take bites
        > out of the string, then included each piece as a hidden variable in my
        > form. When I received the return POST, I reassembled all the strings
        > using the (.) concatenator, then printed this out.
        >
        > The problem is, pieces of the original text are missing, and there are
        > numerous // escape elements present in the text.
        >
        > Here is my code:
        >
        > $questions = $_POST['questions']; // coming from a previous POST
        >
        > $questions=subs tr($questions, 0, 3556); // I put a limit on the total
        > string size.
        >
        > $questions0=sub str($questions, 0, 254);
        > $questions1=sub str($questions, 254, 254);
        > $questions2=sub str($questions, 508, 254);
        > $questions3=sub str($questions, 762, 254); // etc.
        >[/color]
        $i=0;
        while(strlen($q uestion) > 255) {
        $curstr = substr($questio ns, 0, 254);
        Now cut the first 255 characters from $questions
        echo "<INPUT TYPE=\"HIDDEN\" NAME=\"question s$i\" value=\"$curstr \">\n";
        $i++
        }

        [color=blue]
        ><INPUT TYPE="HIDDEN" NAME="questions 0" value="<?=$ques tions0?>" >
        ><INPUT TYPE="HIDDEN" NAME="questions 1" value="<?=$ques tions1?>" >
        ><INPUT TYPE="HIDDEN" NAME="questions 2" value="<?=$ques tions2?>" >
        ><INPUT TYPE="HIDDEN" NAME="questions 3" value="<?=$ques tions3?>" > //
        > etc.
        >
        > Then I POST the data, and then it comes back to my redirect page, which
        > uses the following code:
        >
        > $questions0=$_P OST['questions0'];
        > $questions1=$_P OST['questions1'];
        > $questions2=$_P OST['questions2'];
        > $questions3=$_P OST['questions3']; // etc.
        >
        > $questions=$que stions0 . $questions1 . $questions2 . $questions3 .
        > $questions4 . $questions5 . $questions6 . $questions7 . $questions8 .
        > $questions9 . $questions10 . $questions11 . $questions12 . $questions13
        > . $questions14; // concatenate it
        >
        > print ($questions); // I display $questions. It is mostly there, with
        > parts missing, and many //s here and there.
        >[/color]

        $i=0;
        $varName = "questions" .$i;
        do{
        $questions .= $_POST['$varName'];
        $i++;
        $varName = "questions" .$i;
        }while($_POST['$varName'])


        Or something along those lines is a much neater way of doing what you want to
        do. You're going to have to strip slashes yourself. www.php.net/stripslashes

        db
        --

        /(bb|[^b]{2})/
        Trees with square roots don't have very natural logs.

        Comment

        Working...