AdRock Posted July 28, 2007 Share Posted July 28, 2007 I have a text file which I want to include on the home page of my site but I would only like the first couple of paragraphs. Is there a way I can select only the first 2 paragraphs even if the text file has many paragraphs? This is only the basic version so far that reads the text file and displays it <?php $myFile = "testFile.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); echo $theData; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 28, 2007 Share Posted July 28, 2007 Assuming each para ends with a couple of return characters <?php $text = file_get_contents('testFile.txt'); $paras_2 = array_slice ( explode ("\n\n", $text), 0, 2 ); echo join ('<br><br>', $paras_2); ?> Quote Link to comment Share on other sites More sharing options...
AdRock Posted July 28, 2007 Author Share Posted July 28, 2007 For some reason it's displaying the whole file instead of the first 2 paragraphs Quote Link to comment Share on other sites More sharing options...
Barand Posted July 28, 2007 Share Posted July 28, 2007 Then my initial assumption is wrong. Replace "\n\n" with your paragraph separator. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 28, 2007 Share Posted July 28, 2007 Use the explode() syntax. http://www.php.net/explode Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted July 28, 2007 Share Posted July 28, 2007 Use the explode() syntax. http://www.php.net/explode Read this (code too): Assuming each para ends with a couple of return characters <?php $text = file_get_contents('testFile.txt'); $paras_2 = array_slice ( explode ("\n\n", $text), 0, 2 ); echo join ('<br><br>', $paras_2); ?> ======= Try this: <?php $text = file_get_contents('testFile.txt'); $paras_2 = array_slice ( explode ("\n\n", str_replace(array("\r\n","\r","\n",$text)), 0, 2 ); echo join ('<br><br>', $paras_2); ?> Quote Link to comment Share on other sites More sharing options...
AdRock Posted July 28, 2007 Author Share Posted July 28, 2007 Thanks for your suggestions and I tried them both but still doesn't work Here is an example of what the testFile looks like. Instead of line breaks I used paragraph tags <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer nec orci. Suspendisse ut odio id nibh posuere gravida. Donec malesuada. Vestibulum quis lorem in turpis auctor hendrerit. Nullam diam orci, lobortis ac, commodo vel, ullamcorper nec, mi. Fusce sit amet enim. Pellentesque condimentum est at arcu. Cras condimentum ligula at nisi. Quisque dapibus. Curabitur velit urna, vulputate ut, posuere ac, vehicula ut, erat. Duis placerat feugiat pede. Quisque blandit. Fusce at tellus et ipsum suscipit ornare. Proin ut nunc. Fusce vel nunc. Phasellus lectus augue, pretium nec, viverra eget, tincidunt sit amet, lorem. Sed ut eros.</p> <p>Duis interdum ultricies orci. Phasellus commodo facilisis libero. Praesent ut augue ut nunc porta lacinia. Maecenas nec sem. Nulla aliquet. Sed faucibus scelerisque diam. Curabitur enim velit, luctus quis, congue quis, vestibulum vel, mauris. Suspendisse posuere, massa eget pretium auctor, turpis neque volutpat felis, a laoreet diam quam vitae ipsum. Morbi dapibus aliquet mi. Mauris convallis, ligula quis adipiscing nonummy, ipsum erat lobortis eros, posuere vulputate eros ligula at purus. Vivamus mattis mi et turpis. Aliquam tincidunt adipiscing felis. Aliquam lobortis. Quisque ipsum massa, posuere nec, rutrum non, molestie at, leo. Integer posuere.</p> <p>Integer eros. Donec laoreet massa id nibh. Integer ullamcorper. Donec ac nisl nec enim suscipit condimentum. Etiam ut magna. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Ut convallis gravida mauris. In consequat lorem ut arcu volutpat sagittis. Aenean sagittis molestie neque. Integer massa. Maecenas leo. </p> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.