Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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类来计算txt文件中的单词数_Php_Oop - Fatal编程技术网

PHP类来计算txt文件中的单词数

PHP类来计算txt文件中的单词数,php,oop,Php,Oop,我有一段代码,可以计算给定.txt文件中的单词,但是由于某些原因,我得到了以下错误,并且没有给出结果。我不明白怎么了: 警告:arsort()要求参数1为数组,在第20行的/Applications/MAMP/htdocs/test-php-oop/class.wordcounter.php中为空 警告:第21行/Applications/MAMP/htdocs/test-php-oop/class.wordcounter.php中为foreach()提供的参数无效 <?php // in

我有一段代码,可以计算给定.txt文件中的单词,但是由于某些原因,我得到了以下错误,并且没有给出结果。我不明白怎么了:

警告:arsort()要求参数1为数组,在第20行的/Applications/MAMP/htdocs/test-php-oop/class.wordcounter.php中为空

警告:第21行/Applications/MAMP/htdocs/test-php-oop/class.wordcounter.php中为foreach()提供的参数无效

<?php // index.php
    include_once("class.wordcounter.php");
    $wc = new WordCounter("words.txt");
    $wc->count(WordCounter::DESC);
?>


<?php //class.wordcounter.php

class WordCounter {

    const ASC = 1;
    const DESC = 2;

    private $words;

    function __contruct($filename) {
        $file_content = file_get_contents($filename);
        $this->words = (array_count_values(str_word_count(strtolower($file_content),1)));

    }

    public function count($order) {
        if ($order == self::ASC)
            asort($this->words);
        else if ($order == self::DESC)
            arsort($this->words);
        foreach ($this->words as $key => $val)
            echo $key . " = " . $val . "<br/>";
    }

}

?>

脚本似乎无法获取指定的文件。请尝试使用绝对URL,并让我知道它是否有效

更改此行:

$wc = new WordCounter("http://your_domain/your_path/words.txt");

您确定
file\u get\u contents()
确实返回了一些内容吗?仅仅因为你看到了那里的文件并不意味着PHP一定可以访问它。

我发现了这个bug,这个
\u构造方法拼写错误

下面是正确的代码,它可以工作:

<?php // index.php
    include_once("class.wordcounter.php");
    $wc = new WordCounter("words.txt");
    $wc->count(WordCounter::DESC);
?>


<?php //class.wordcounter.php

class WordCounter {

    const ASC = 1;
    const DESC = 2;

    private $words;

    function __construct($filename) {
        $file_content = file_get_contents($filename);
        $this->words = (array_count_values(str_word_count(strtolower($file_content),1)));

    }

    public function count($order) {
        if ($order == self::ASC)
            asort($this->words);
        else if ($order == self::DESC)
            arsort($this->words);
        foreach ($this->words as $key => $val)
            echo $key . " = " . $val . "<br/>";
    }

}

?>

试试这个

<?php
$no_of_words = 0;
str_replace(" ","",file_get_contents("/filename.txt"),$no_of_words);
echo $no_of_words;
?>


此错误意味着:
$This->words
不是数组,但应该是数组。请尝试在构造函数中的$This->words上使用var\u dump;我假设$file\u内容为空?我试图检查该文件是否未被获取,但它是空的,并且我检查了它是否作为数组返回,它是空的..那又怎样?我在MAMPWhy有人投票-1??我认为这是一段非常有用的代码,一旦调试成功,你就拼错了u_构造方法。我试图直接用file_get_内容调用该文件,而没有通过类传递,它就工作了。因此,我认为这不是路径问题。尝试使用file()而不是file_get_contents()以数组形式检索文件内容:顺便说一句,尝试在$file_content=file_get_contents($filename)之后添加var_dump($file_content)进行调试;并在您的帖子中发布输出。我确信我尝试过在不使用类的情况下通过file\u get\u contents直接调用该文件,并且效果很好:/