Skip to content

Instantly share code, notes, and snippets.

@amin3d
Created May 15, 2017 09:19
Show Gist options
  • Save amin3d/69d0e09b6ae5b1559b5db13245c4e95c to your computer and use it in GitHub Desktop.
Save amin3d/69d0e09b6ae5b1559b5db13245c4e95c to your computer and use it in GitHub Desktop.
Plain text to paragraph
function paragraph_formater($content,$limit)
{
$content = array_filter(preg_split('/(?<!\.)\.\s*(?!\.)/', $content));
$out = [];
$temp = '';
for ($i = 0; $i < count($content); $i++) {
$len = mb_strlen($content[$i]);
if ($temp == '') {
if ($len > $limit) {
$out[] = '<p>' . $content[$i] . '.</p>';
} else {
$temp .= $content[$i] . '.';
}
} else {
$templen = mb_strlen($temp);
if (($len + $templen) > $limit) {
$out[] = '<p>' . $temp . $content[$i] . '.</p>';
$temp = '';
} else {
$temp .= $content[$i] . '.';
}
}
}
return implode('',$out);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment