Split a text by its paragraphs
This is how you can split a text by its natural paragraphs using line break characters backslash r and backslash n (\r\n). This script looks for three following line breaks and add it to an array for other actions. This code is ripped out of its context in a Drupal module.
<?php
$node = node_load($nid);
$body = $node->body;
$body = strip_tags($body);
preg_match_all("/[\r\n]/", $body, $subs, PREG_OFFSET_CAPTURE);
foreach ($subs[0] as $key => $value) {
if ($value[1] == (($subs[0][$key+1][1]) - 1) AND $value[1] == (($subs[0][$key+2][1]) - 2)) {
$limits[] = $value[1];
}
}
$limits[] = strlen($body);
$from = 0;
$paragraphs = array();
foreach ($limits as $r => $value) {
$paragraphs[] = trim(substr($body, $from, ($value-$from)));
$from = $value;
}
?>
<pre>
<?php
print_r($paragraphs);
?>
</pre>
<?php
?>
And here is a ready to go function
<?php
/**
* Split text into natural paragraphs by tripple newline
*
* @param string $body
* @return array
*/
function _get_paragraphs($body) {
$paragraphs = array();
preg_match_all("/[\r\n]/", $body, $subs, PREG_OFFSET_CAPTURE);
foreach ($subs[0] as $key => $value) {
if ($value[1] == (($subs[0][$key+1][1]) - 1) AND $value[1] == (($subs[0][$key+2][1]) - 2)) {
$limits[] = $value[1];
}
}
$limits[] = strlen($body);
$from = 0;
foreach ($limits as $r => $value) {
$paragraphs[] = trim(substr($body, $from, ($value-$from)));
$from = $value;
}
return $paragraphs;
}
?>
Knowledge keywords: