Thursday, April 18, 2013

PHP: LOOP THROUGH EACH STRING LINE IN A TEXTAREA


To loop through all the string inputs entered on a new line for a textarea control is not particularly challenging and uses basic array functionality to achieve this.
To see the code in action:
//trim off excess whitespace off the whole
$text = trim($_POST['textareaname']);

//explode all separate lines into an array
$textAr = explode("\n", $text);

//trim all lines contained in the array.
$textAr = array_filter($textAr, 'trim');

//loop through the lines
foreach($textAr as $line){
echo "$line";
}

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. i have following string
    "10:43 AM\nNUjjwa\n 91 12345 67890\nMany many happy returns of the day"
    and i want to read each new line after "\n" and store in some variable.

    how i do this????

    ReplyDelete