如何使用正则表达式和PHP获取数组中文章的所有段落?

时间:2010-12-29 09:14:34

标签: php regex

请帮我从数组中的文章中获取所有段落。该段落不包含html。我只需要通过换行符分隔段落。请注意,文章可能有多个换行符。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

检测换行符的小程序&放入一个数组:

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

答案 2 :(得分:0)

这个怎么样

$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