Jump to content

[SOLVED] How to select the first 2 paragraphs from a text file


AdRock

Recommended Posts

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;			
?>

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.