且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算textarea中的新行以调整PHP中的容器大小?

更新时间:2023-12-04 13:05:46

preg_match_all(/(\\\
)/,$ text,$ matches); $ p $

  preg_match_all 
$ count = count($ matches [0])+ 1; // +1为最后的齿



编辑:由于您使用nl2br所以' \\\
'被替换为< br> 。所以你需要这个代码。



  preg_match_all(/(< br>)/,$ text,$火柴); 
$ count = count($ matches [0])+ 1; // $为最后的齿



然而,< br> 不会在textarea中显示为换行符(如果我没有记错的话),因此您可能需要删除 nl2br



希望这有助于。


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));

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

Is there a way to do this...

I am still learning so thanks for your help...

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; // +1 for 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.