By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

Correct string-split and reassemble?

P: n/a
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=substr($questions, 0, 3556); // I put a limit on the total
string size.

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

<INPUT TYPE="HIDDEN" NAME="questions0" value="<?=$questions0?>" >
<INPUT TYPE="HIDDEN" NAME="questions1" value="<?=$questions1?>" >
<INPUT TYPE="HIDDEN" NAME="questions2" value="<?=$questions2?>" >
<INPUT TYPE="HIDDEN" NAME="questions3" value="<?=$questions3?>" > //
etc.

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

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

$questions=$questions0 . $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.

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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_var), so if you have any spaces in the input remove it
before any other process.
-----------------------------------------
but you may can concatenate your text using implode(".",$varaible)
if your still in problem let me know we may also use REGEXP to check
the data, see u.

Jul 17 '05 #2

P: n/a
Robert wrote:
The problem is, pieces of the original text are missing, and there are
numerous // escape elements present in the text.
Do you mean extra \ (backslashes)?
I suspect you (or the credit-card provider) are running with
"magic_qutes_gpc" set.

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

http://www.php.net/manual/en/function.stripslashes.php


A few comments about your code (snippets not tested):
$questions0=substr($questions, 0, 254);
$questions1=substr($questions, 254, 254);
$questions2=substr($questions, 508, 254);
$questions3=substr($questions, 762, 254); // etc.
Ugh!

define('CHUNK_SIZE', 254);
$chunks = 1 + strlen($questions)/CHUNK_SIZE;
for ($i = 0; $i < $chunks; ++$i) {
/* using variable variables */
$q = 'questions' . $i;
$$q = substr($questions, CHUNK_SIZE * $i, CHUNK_SIZE);
}
IMO, even better would be to use arrays:

define('CHUNK_SIZE', 254);
$qs = array();
$chunks = 1 + strlen($questions)/CHUNK_SIZE;
for ($i = 0; $i < $chunks; ++$i) {
$qs[] = substr($questions, CHUNK_SIZE * $i, CHUNK_SIZE);
}
<INPUT TYPE="HIDDEN" NAME="questions0" value="<?=$questions0?>" >
<INPUT TYPE="HIDDEN" NAME="questions1" value="<?=$questions1?>" >
<INPUT TYPE="HIDDEN" NAME="questions2" value="<?=$questions2?>" >
<INPUT TYPE="HIDDEN" NAME="questions3" value="<?=$questions3?>" > //
etc.
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, '"/>';
}
Then I POST the data, and then it comes back to my redirect page, which
uses the following code:

$questions=$questions0 . $questions1 . $questions2 . $questions3 .
$questions4 . $questions5 . $questions6 . $questions7 . $questions8 .
$questions9 . $questions10 . $questions11 . $questions12 . $questions13
. $questions14; // concatenate it
$questions = implode('', $_POST['questions']);
print ($questions); // I display $questions. It is mostly there, with
parts missing, and many //s here and there.

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!
Jul 17 '05 #3

P: n/a
An noise sounding like Robert said:
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=substr($questions, 0, 3556); // I put a limit on the total
string size.

$questions0=substr($questions, 0, 254);
$questions1=substr($questions, 254, 254);
$questions2=substr($questions, 508, 254);
$questions3=substr($questions, 762, 254); // etc.
$i=0;
while(strlen($question) > 255) {
$curstr = substr($questions, 0, 254);
Now cut the first 255 characters from $questions
echo "<INPUT TYPE=\"HIDDEN\" NAME=\"questions$i\" value=\"$curstr\">\n";
$i++
}

<INPUT TYPE="HIDDEN" NAME="questions0" value="<?=$questions0?>" >
<INPUT TYPE="HIDDEN" NAME="questions1" value="<?=$questions1?>" >
<INPUT TYPE="HIDDEN" NAME="questions2" value="<?=$questions2?>" >
<INPUT TYPE="HIDDEN" NAME="questions3" value="<?=$questions3?>" > //
etc.

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

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

$questions=$questions0 . $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.


$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.

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.