Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中使用substr_count()计算空白_Php - Fatal编程技术网

如何在PHP中使用substr_count()计算空白

如何在PHP中使用substr_count()计算空白,php,Php,如何使用PHP计算文件中的空格数 我尝试了substr函数,但它不起作用。一种方法是删除所有其他字符,然后计算剩余的字符数。您可以通过以下方法来实现这一点: $count_var = preg_replace('[^\s]', '', $string); $count = strlen($count_var); 一种方法是删除所有其他字符并计算剩余的字符数。您可以通过以下方法来实现这一点: $count_var = preg_replace('[^\s]', '', $string); $co

如何使用PHP计算文件中的空格数


我尝试了substr函数,但它不起作用。

一种方法是删除所有其他字符,然后计算剩余的字符数。您可以通过以下方法来实现这一点:

$count_var = preg_replace('[^\s]', '', $string);
$count = strlen($count_var);

一种方法是删除所有其他字符并计算剩余的字符数。您可以通过以下方法来实现这一点:

$count_var = preg_replace('[^\s]', '', $string);
$count = strlen($count_var);

将任何非空白替换为零,计算结果:

echo strlen(preg_replace('/\S/', '', $text));
这适用于任何空白,包括制表符等。
substr\u count
应该可以正常工作,但对于常规空格:

echo substr_count($text, ' ');

将任何非空白替换为零,计算结果:

echo strlen(preg_replace('/\S/', '', $text));
这适用于任何空白,包括制表符等。
substr\u count
应该可以正常工作,但对于常规空格:

echo substr_count($text, ' ');

int子序列计数(字符串$haystack,字符串$needle[,int$offset=0[,int$length])


intsubstr\u count(字符串$haystack,字符串$needle[,int$offset=0[,int$length])


使用:

$test = "sadlk asd sad sda";
$whitespaces = substr_count($test," ");
使用: