Php完全分割txt文件

时间:2014-03-30 22:56:18

标签: php file if-statement foreach explode

在我的文本文件中,我有这样的句子:

单词单词单词。 twoword词单词词。 三字词单词词。 四字词词词。 五字词字词。

我希望完全停止分割文本文件,并将每个句子放在一个新的文本文件中。我知道我必须使用file_put_content和foreach语句,但我正在努力解决它。 请帮忙!

2 个答案:

答案 0 :(得分:3)

您可以使用explode()通过句号分割内容:

$text = file_get_contents('file.txt');
$sentences = explode('.', $text);

// do stuff with sentences array

答案 1 :(得分:0)

$text = "word word word word word. twoword word word word word. threeword word word word word. fourword word word word word. fiveword word word word word.";

$splitted_text = explode(".", $text);


for($i = 0; $i < count($splitted_text); $i++){
    file_put_contents("file_$i", $splitted_text[$i].'.');

    //to display a file just use echo file_get_contents($file_name) as such :
    echo "file $i :<br/>";
    echo file_get_contents("file_$i"); //note that you could echo $splitted_text[$i]
    echo "<hr/>";
}