how to get all the paragraphs of an article in an array using regex and PHP?
Please help me to get all the paragraphs from an article in an array. The paragraph contains no html. I just need to separate th开发者_JS百科e paragraph through line breaks. Note an article may have multi line breaks.
A small program that detects line breaks & puts in an array :
<?php
$text = 'Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum has been the industrys s
tandard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it
to make a type specimen book. It has survived not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset
sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus
PageMaker including versions of Lorem Ipsum.';
$splitted_para_arr = preg_split("/[\n]+/",$text);
echo '<pre>';
print_r($splitted_para_arr);
echo '</pre>';
?>
$article = 'line1
line2
line3
line4 line4 line4
line10
';
//replace multiple linebreaks
//( trim it too and add a new line at the beginig of the string )
$article = "\n" . preg_replace('/\n{2,}/', "\n", trim($article));
var_dump($article);
//match all lines
preg_match_all('/\n(.*)/', $article, $matches);
var_dump($matches);
what about this
$para="This is line one!
This is another line.";
$x=explode("\n",$para);
echo "<pre>";
print_r($x);
now use array_filter() for the string length >1
精彩评论