Bleach
Bleach

Reputation: 23

Processing each line of a textarea form in a script

I've learned it's possible to trim a string from a textarea and put break tags after it, so each sentence written at a new line in the textbox will also be written at a new line in the PHP file.

This is the snippet:

<html>
<body>

<?php
$text = trim($_POST['textarea']);
$text = nl2br($text);
echo $text;
?>

</body>
</html>

The thing is that my true intentions are:

  1. Use the contents of each line in the textbox for a certain script

  2. Print the contents of each line with the results from the script added all separated by lines.

Upvotes: 2

Views: 7327

Answers (2)

greg606
greg606

Reputation: 491

<?php
    $text = trim($_POST['textarea']);
    $text = explode ("\n", $text);

    foreach ($text as $line) {
       echo myFunction($line);
       echo "< hr />";
    }
?>

Upvotes: 5

Sir Lojik
Sir Lojik

Reputation: 1429

The PHP function explode lets you take a string and blow it up into smaller pieces. Store this array for use in other scripts

$str_arr = explode("\n", $_POST['textarea']);
//$str_arr can be used for other script

Upvotes: 0

Related Questions