Luis
Luis

Reputation: 1097

Parse each line in PHP

I want to create like a notepad in js and php but I want it to add some spaces for each new line I thought I can make it with a textarea, but I dont know how to This is my idea:

<textarea name="text"></textarea>

and in PHP

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

foreach ($textAr as $line) {
    $height = $height + $line_height; 
} 

But not really sure it works. Any idea?

Upvotes: 2

Views: 153

Answers (3)

RiaD
RiaD

Reputation: 47620

$textAr = array_filter($text, 'trim'); seems to be wrong.
  1. You use $text while you already have $textAr and want use it
  2. Seems you need array_map, not array_filter

array_map Manual

Upvotes: 1

Najzero
Najzero

Reputation: 3202

Maybe my brain is already sleeping, but did you maybe mean

$textAr = array_filter($textAr, 'trim');

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

You are basically discarding the explode step. Try this instead:

$textAr = array_filter(preg_split("/[\r\n]+/",$_POST['text']));

This basically allows any format of newline, and removes empty lines. You can then pass $textAr through foreach.

Upvotes: 0

Related Questions