john890
john890

Reputation: 13

Php Splitting a txt file by full stop

In my text file I have sentences like this:

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.

I want to split the text file by full stop and have each sentence in a new text file . I know I have to use file_put_content and foreach statement but I am struggling with getting my head around it. Any help please!

Upvotes: 0

Views: 1300

Answers (2)

Loïc
Loïc

Reputation: 11943

$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/>";
}

Upvotes: 0

Cjmarkham
Cjmarkham

Reputation: 9681

You could use explode() to split the content via the full stop:

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

// do stuff with sentences array

Upvotes: 3

Related Questions