Patricia Smith
Patricia Smith

Reputation: 39

php read from textarea each line doesn't work why?

I'm trying to create a code read from textarea each line in Array like print $line[1] ; print $line[2] ;

I replaced print with echo but syill doesn't work

<form method="post" action="test.php">
    <textarea rows="4" cols="50" name="textareaname">
Line one test
Line 2 test
</textarea>
<input type="submit" value="Submit">
</form>

<?php
$text =  = trim($_POST['textareaname']);
$textAr = explode("\n", $text);
$textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind

foreach ($textAr as $line) {
    // processing here
} 
print $line[1] ;
print $line[2] ;
?>

HTTP ERROR 500

Upvotes: 1

Views: 332

Answers (1)

u_mulder
u_mulder

Reputation: 54841

$line is variable that holds the last element (and it is string) of $textAr after the foreach loop is over. And using $line[1] prints second symbol of the string.

What you really want to print and see is $textAr[0] or $textAr[1]:

print $textArr[1];

This will print required element of your $textArr array.

Update: if you need to send email assuming that each line is valid address you can use this code:

foreach ($textAr as $line) {
    echo 'Current email is ' . $line;
    // sending email code goes here
} 

Upvotes: 1

Related Questions