从php中的内容显示前两段

I want to display first two paragraphs from a content in a particular page. i know it could be done on the basis of number of characters. Is there any possible way to display it based on the paragraphs. The content is purely text.

use :

substr($paragraph, 0, $nb_characters);

explode function witth " " as separator :

$arrTxt = explode("
", $string);

Example:

$string = "Line 1....
line 2
Another line";
$arrTxt = explode("
", $string);
var_dump($arr);

Result:

0 => string 'Line 1....' 1 => string 'line 2' 2 => string 'Another line'

This may return empty if there is a double return, so it is probably better to do:

$paragraphs = preg_split('/$\R?^:/m', $string, -1, PREG_SPLIT_NO_EMPTY);

(UPDATED AGAIN - this will cover the 2 type of line breaks - or )

This should then contain an array of non-empty strings representing your paragraphs.

You can split whole string into chunks using " " and just use first two like below

$array = explode("
", $data, 2);
echo $array[0] //paragraph 1
echo $array[1] //paragraph 2

Split your content by carriage return " " using explode() function. This will give you an array of paragraphs. Then you will only need to display first two values in the array.

$contentArray = explode("
", $content);
echo $contentArray[0] . $contentArray[1];

Please note that carriage return could differ from system to system. It could be " " or " ". You will need to test that.

Hope this helps.