Split text for sentences with PHP PDF Print E-mail

Getting sentences from text if function needed in many applications.

There i will describe how to split text for sentences with PHP.

 

I usually use next function to parse text for sentences:

function toxttosentence($text){

$text=ereg_replace("\t","",$text);            
$text = preg_replace("/\n\s+\n/", "\n\n", $text);
$text = preg_replace("/[\n]{3,}/", "\n\n", $text);

$sentences=array();
$a=split("\n\n",$text);    
foreach ($a as $b)
{
 $b = preg_replace("/http:\/\/(.*?)[\s\)]/", "", $b);
 $b = preg_replace("/http:\/\/([^\s]*?)$/", "", $b);
 $b = preg_replace("/\[\s*[0-9]*\s*\]/", "", $b);
 foreach (split('\.',$b) as $sent)
 if(strlen(trim($sent))>3){
 $sent=preg_replace("/\n/", " ", $sent);
 $sent=trim(ereg_replace("  ", " ", $sent));
 $sent[0]=strtoupper($sent[0]);
 array_push($sentences,$sent.'.');
 }
}

return $sentences;
}

Last Updated on Friday, 19 March 2010 11:43
 

Add comment


Security code
Refresh