note 83383 deleted from function.ucwords by cmb
| From: | cmb@php.net | Date: | Sun, 24 Nov 2019 09:07:42 +0000 |
| Subject: | note 83383 deleted from function.ucwords by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213234@lists.php.net to get a copy of this message | ||
Note Submitter: gregomm at gmail dot com
----
An improved of ucsentence. In fact its a function to avoid ugly data entry. It's based on code
taken from this page. Can be inproved for better performance, of course...
Features:
1- removes duplicated question marks, an exclamations
2- Capitalize first letter of a sentence.
3- split sentences not only with "." but also with "?" and "!"
4- Puts a white space at the begining of each sentence
5- undestand the meaning of "¡" and "¿" in languages like spanish.
6- undestand the htmlentitity version of this simbols.
function sentenceNormalizer( $sentence_split ) {
$sentence_split = preg_replace("/[!]+/","!",$sentence_split);
$sentence_split = preg_replace("/[¡]+/","¡",$sentence_split);
$sentence_split = preg_replace("/[?]+/","?",$sentence_split);
$sentence_split = preg_replace("/[¿]+/","¿",$sentence_split);
$textbad = preg_split("/(\<[a-zA-Z0-9-]*\>".
"\!(\s)?|\.(\s)?|\?(\s)?|¿(\s)?|¡(\s)?".
"|¿(\s)?|¡(\s)?)/",
$sentence_split,-1,PREG_SPLIT_DELIM_CAPTURE);
$newtext = array();
$count = sizeof($textbad);
$prevStr ="";
for ($i = 0; $i < $count; $i++){
$text = trim($textbad[$i]);
$size = strlen($text);
if ($size>1){
$sentencegood=ucfirst(strtolower($text));
if ($i>0 && $prevStr != '¿' && $prevStr != '¡'
&& $prevStr !="¿" && $prevStr !="¡"){
$sentencegood =" ".$sentencegood;
}
$newtext[] = $sentencegood;
$prevStr =$text;
}elseif($size==1 ){
if ($i>0 && ($text == '¿' || $text == '¡' || $prevStr
=="¿" || $prevStr =="¡") ){
$newtext[] =" ".$text;
}else{
$newtext[] =$text;
}
$prevStr =$text;
}
}
$textgood = implode($newtext);
return $textgood;
}