[英]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.
该段不包含html。 I just need to separate the paragraph through line breaks.
我只需要通过换行符分隔该段。 Note an article may have multi line breaks.
请注意,文章可能会有多个换行符。
$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);
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>';
?>
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.