Created
May 15, 2017 09:19
-
-
Save amin3d/69d0e09b6ae5b1559b5db13245c4e95c to your computer and use it in GitHub Desktop.
Plain text to paragraph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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