Count New Lines In Textarea To Resize Container In Php?
I have a textarea submitted to a php file. Im using this to make it look exactly as the user entered the text: $ad_text=nl2br(wordwrap($_POST['annonsera_text'], 47, '\n', true));
Solution 1:
You want the substr_count function.
Solution 2:
You can use regular expression:
preg_match_all("/(\n)/", $text, $matches);
$count= count($matches[0]) +1; // +1 for the last tine
EDIT: Since you use nl2br so '\n
' is replaced by <br>
. So you need this code.
preg_match_all("/(<br>)/", $text, $matches);
$count = count($matches[0]) + 1; // +1for the last tine
However, <br>
will not be display as newline in textarea (if I remember correctly) so you may need to remove nl2br
.
Hope this helps.
Solution 3:
$lines= explode("\n", $text);
$count= count($lines);
$html= implode($lines, "<br>");
Post a Comment for "Count New Lines In Textarea To Resize Container In Php?"