Read newlines in textarea to resize container in PHP?

I have a text box sent to a php file.

I use this to make it look exactly like the user entered text:

$ad_text=nl2br(wordwrap($_POST['annonsera_text'], 47, "\n", true)); 

If I want to resize the container, I must be able to read how many lines are in the $ ad_text variable.

Is there any way to do this ...

I am still so grateful for your help ...

0
source share
3 answers

You can use regex:

 preg_match_all("/(\n)/", $text, $matches); $count = count($matches[0]) + 1; // +1 for the last tine 

EDIT: Since you are using nl2br, ' \n ' is replaced with <br> . So you need this code.

 preg_match_all("/(<br>)/", $text, $matches); $count = count($matches[0]) + 1; // +1 for the last tine 

However, <br> will not appear as a new line in textarea (if I remember correctly), so you may need to remove nl2br .

Hope this helps.

0
source
+3
source
 $lines = explode("\n", $text); $count = count($lines); $html = implode($lines, "<br>"); 
0
source

Source: https://habr.com/ru/post/900444/


All Articles