user1162926
user1162926

Reputation: 41

Eachline with textarea?

What I'm trying to do is take the text from a textarea using the $_POST method, and write each line with '-' at the start. This is what I got so far.

$lines = $_POST["textarea"];
foreach ($lines as $line)
    echo " - " . $line . "<br />\n";

(This is taken from php.net, I haven't programmed in PHP so long) When I run it, this is all I get:

Warning: Invalid argument supplied for foreach() in I:\xampp\htdocs\generate.php

Any help would be appreciated :)

Upvotes: 2

Views: 169

Answers (2)

Jon
Jon

Reputation: 437386

foreach expects an array as its first argument. You are passing in $lines which is a string (possibly containing newline characters).

To process each line separately, first you have to split the input into an array of lines. You can do that with

$lines = explode("\n", $_POST["textarea"]);

The function explode splits the input string into an array of substrings delimited by whatever you pass as the first parameter.

Upvotes: 4

timing
timing

Reputation: 6455

$_POST["textarea"]; is no array. you have to split on the newline characters first:

$lines = explode("\n", $_POST['textarea']);

Upvotes: 1

Related Questions