Cyclone Cyclone - 4 years ago 119
PHP Question

Count lines in a posted string

I've tried this: count new lines in textarea to resize container in PHP?

But it doesn't seems to be working:

$content = nl2br($_POST['text']);
preg_match_all("/(<br>)/", $content, $matches);

echo count($matches[0]) + 1;

It'll always output

Is there any other solutions to count lines in a string?

Answer Source

Found this in one of my old apps... Not sure where I got it from.

$lines_arr = preg_split('/\n|\r/',$str);
$num_newlines = count($lines_arr); 
echo $num_newlines;

*Edit - Actually, this probably won't work if your textarea is spitting out html.. check for <br> and <br/>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download