如何将文章文本文件读取为字符串


How to read article text file to string?

我希望PHP像这样阅读我的文章文本文件。

示例文本文件:

OMG! Where is my right hand.
I try to find my right hand but I can't see it.
please tell me how to find it.

现在我有这个函数代码

function getContent($file_path,$path=''){
        $file_path = $file_path;
        if(file_exists('./'.$file_path)){
            $f_read = fopen($path.$file_path,'r');
            $rs = "";
            while (!feof($f_read)) {
                $rs .= fread($f_read, 8192);
            }
            fclose($f_read);
        }
        else{
            echo $rs = "Not Connect File !";
        }
        return($rs);
    }

使用该代码后:

OMG! Where is my right hand. I try to find my right hand but I can't see it. please tell me how to find it.

我想使用 PHP 函数将第一行读取为 string1,第一行之后是 string2,如下所示

$string1 = "OMG! Where is my right hand."
$string2 = "I try to find my right hand but I can't see it.
    please tell me how to find it."

请帮帮我:)

您可以使用

以下代码将段落拆分为字符串,还可以分配字符(如句点、逗号等),您希望从哪里拆分段落。

preg_split('/[.?!]/',$mystring);

您可以参考此链接以获取更多信息:在 PHP 中将段落分解为句子

http://php.net/manual/en/function.explode.php

您可以使用$string=@file_get_contents($path_name); $string=@explode("'n",$string); //for get each line.

为了

将第一行读入 $string 1 并将文件的其余部分读入 $string 2 ...

阅读第一行,就像上面一样,然后致电file_get_contents以获取其余部分。

使用 offset 参数告诉 file_get_contents() 在第一行结束之后开始读取(传递第一行的字符串长度)。

尝试使用PHP的explode()函数

function getContent($file_path,$path=''){
            $file_path = $file_path;
            if(file_exists('./'.$file_path)){
                $f_read = fopen($path.$file_path,'r');
                $rs = "";
                while (!feof($f_read)) {
                    $rs .= fread($f_read, 8192);
                    $demo=explode('.', $rs);
                }
                fclose($f_read);
            }
            else{
                echo $rs = "Not Connect File !";
            }
            return($demo);//result wiil be stored in $demo array like $demo[0], $demo[1]
        }