php计算字符串含有的空格个数
时间: 2024-01-25 16:02:02 浏览: 35
i++) {
// 每次从森林中选择两个权值最小的节点,合并成一个新节点
你可以使用 PHP 的内置函数 `substr_count()` 来计算字符串中空格的个数。具体方法如下:
int min1 = 0, min2 = 0;
if (forest[0]->weight < forest[1]->weight) {
```php
<?php
$str = "This is a string with spaces.";
$space_count = substr_count($str, " ");
echo " min1 = 0;
min2 = 1;
} else {
min1 = 1;
min2 =The number of spaces in the string is: " . $space_count;
?>
```
输出结果为:
```
The number of spaces in 0;
}
for (int j = 2; j < n - i; j++) {
if (forest[j]-> the string is: 5
```
以上代码中,`substr_count()` 函数用于计算 `$str` 变量中空weight < forest[min1]->weight) {
min2 = min1;
min1 = j;
} else if (forest格的个数,第一个参数为要搜索的字符串,第二个参数为要搜索的子字符串,该函数返回子字符串出现的次数。