计数在PHP正则expression式模式的话?

计数在PHP正则expression式模式的话?,第1张

概述计数在PHP正则expression式模式的话?

我试图在linux中匹配来自'/ usr / share / dict / words'的模式'lly',我可以在浏览器中显示它们。 我想要统计与模式匹配的字数,并在输出结束时显示总数。 这是我的PHP脚本。

<?PHP $dfile = fopen("/usr/share/dict/words","r"); while(!feof($dfile)) { $mynextline = fgets($dfile); if (preg_match("/lly/",$mynextline)) echo "$mynextline<br>"; } ?>

只有存在查询string时,才会对图像文件请求重写规则

在sed中匹配的模式后面追加一行不起作用

使用grep来查找dynamic文本

如何在proftpd中使用目录path的正则Expression式

匹配URL的path,减去文件扩展名

您可以使用count函数来计算它们的数组元素数量。 所以你只需要每次添加到这个数组,然后数一下。

<?PHP $dfile = fopen("/usr/share/dict/words","r"); //Create an empty array $array_to_count = array(); while(!feof($dfile)) { $mynextline = fgets($dfile); if (preg_match("/lly/",$mynextline)){ echo "$mynextline<br>"; //Add it to the array $array_to_count[] = $mynextline; } } //Now we're at the end so show the amount echo count($array_to_count); ?>

如果你不想存储所有的值(可能派上用场,但无论如何),一个更简单的方法是增加一个整数变量,如下所示:

<?PHP $dfile = fopen("/usr/share/dict/words","r"); //Create an integer variable $count = 0; while(!feof($dfile)) { $mynextline = fgets($dfile); if (preg_match("/lly/",$mynextline)){ echo "$mynextline<br>"; //Add it to the var $count++; } } //Show the number here echo $count; ?>

PHP:Glob – 手册

sizeof(glob("/lly/*"));

@编辑

另外,你可以这样做:

$array = glob("/usr/share/dict/words/lly/*") foreach ($array as $row) { echo $row.'<br>'; } echo count($array);

总结

以上是内存溢出为你收集整理的计数在PHP正则expression式模式的话?全部内容,希望文章能够帮你解决计数在PHP正则expression式模式的话?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1292849.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-10
下一篇2022-06-10

发表评论

登录后才能评论

评论列表(0条)

    保存