Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count lines in a posted string

Tags:

php

count

lines

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 1.

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

like image 260
Cyclone Avatar asked Oct 31 '11 14:10

Cyclone


People also ask

How do I count the number of lines in a string in PHP?

$filePath = "test. txt" ; $lines = count (file( $filePath ));

How do you count new lines in Python?

Use readlines() to get Line Count This is the most straightforward way to count the number of lines in a text file in Python. The readlines() method reads all lines from a file and stores it in a list. Next, use the len() function to find the length of the list which is nothing but total lines present in a file.

How do I count lines in HTML?

Check out the function getClientRects() which can be used to count the number of lines in an element.


1 Answers

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/>

like image 200
K2xL Avatar answered Sep 20 '22 13:09

K2xL