gpt4 book ai didi

php - 计算已发布字符串中的行数

转载 作者:可可西里 更新时间:2023-11-01 12:41:32 25 4
gpt4 key购买 nike

我试过这个:count new lines in textarea to resize container in PHP?

但它似乎不起作用:

$content = nl2br($_POST['text']);
preg_match_all("/(<br>)/", $content, $matches);

echo count($matches[0]) + 1;

它总是输出1

是否有任何其他解决方案来计算字符串中的行数?

最佳答案

在我的一个旧应用程序中找到这个...不知道我从哪里得到的。

$lines_arr = preg_split('/\n|\r/',$str);
$num_newlines = count($lines_arr);
echo $num_newlines;

*编辑 - 实际上,如果您的文本区域正在吐出 html,这可能不会起作用。检查 <br> and <br/>

关于php - 计算已发布字符串中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7955402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com