计算textarea中的新行以调整PHP中的容器大小? [英] count new lines in textarea to resize container in PHP?

查看:140
本文介绍了计算textarea中的新行以调整PHP中的容器大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个textarea提交给一个php文件。



我使用它来使它看起来完全像用户输入文本一样:

  $ ad_text = nl2br(wordwrap($ _ POST ['annonsera_text'],47,\\\
,true));

如果我想调整一个容器的大小,我必须能够读取变量中有多少行' $ ad_text'。



有没有办法做到这一点...



我还在学习谢谢你可以使用正则表达式:



$ b

解决方案

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.

这篇关于计算textarea中的新行以调整PHP中的容器大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆