我必须计算" a' s"在字符串中:
$a = "aaa apple";
这是我的代码:
<?php
$file = fopen("count.txt","w");
$txt = "as a teacher i am addicted to apple";
fwrite($file,$txt);
fclose($file);
$file1 = fopen("count.txt","r");
$arr = substr_count("$file1","a");
echo $arr;
?>
但是这会返回0.
我应该使用哪个php函数?
答案 0 :(得分:0)
您正在使用
$arr = substr_count("$file1", "a");
而不是
$arr = substr_count($txt, $a);
为什么在检查出现次数之前需要写入文件?
如果您想再次获取该文件的内容,请使用
$file1 = fopen("count.txt","r");
$contents = fread($file1, filesize("count.txt"));
fclose($file1);
$arr = substr_count($contents, $a);