0

I have been working on this for a while and cannot seem to figure it out at all. Any help would be appreciated. here we go.

I have an html form that has a text box and a submit button. the text entered in the text box is posted to my .php processor form. Once it gets here, I use:

$textdata = $_POST['textdata'];

$input = explode("\n", $textdata);

this takes the data, splits it by line, and stores each line in an array called $input. from here i can echo $input[0] to get the first line and so on. But I need to use this further down in my script and need to assign a variable to the first line, or $input[0]. $input[0] = $line1; does not work. I think I might have to use extract() and a foreach loop? Any help would be greatly appreciated. thanks!

2
  • What you're describing should work. Can you try var_dump()'ing the $input array before and after modifying the first element to make sure? Commented Apr 11, 2011 at 2:16
  • 1
    Have you verified $input is in fact an array? Depending on server settings/encoding, the newline character may not be present, so your explode wouldn't work as-is. Commented Apr 11, 2011 at 2:18

3 Answers 3

2

well fo one thing $input array will always be available, or what you can do if i understand correctly is:

$textdata = $_POST['textdata'];

$input = explode("\n", $textdata); //this should have the array of lines assuming
                                   //that $textdata was \n delimited

$line1 = $input[0]; //use $line1 later in code
Sign up to request clarification or add additional context in comments.

2 Comments

wow, I was defining my variable backwards. that worked perfectly thank you so much.
You may want to tick this answer if it solved your problem :)
1
$line1 = $input[0];
$line2 = $input[1];
$line3 = $input[2];
// etc.

or:

for ($i=0, $inputlen = count($input); $i < $inputlen; $i++) {
    ${'line'.($i+1)} = $input[$i];
}

or simply:

list($line1, $line2, $line3) = $input;

Comments

0

$input[0]. $input[0] = $line1;

I can't tell if the full-stop in that line is a full-stop or concatenation operator.

For concatenation, it should be this way.

$input[0] = $input[0] . $line1;

or even shorter

$input[0] .= $line1;

If you're just wanting to assign $input[0] to $line1 by value, it's

$line1 = $input[0];

You can also assign a reference using

$line1 =& $input[0];

Using the latter, any changes to $line1 will be present in $input[0].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.