note 83383 added to function.ucwords

From: Date: Fri, 23 May 2008 07:38:21 +0000
Subject: note 83383 added to function.ucwords
Groups: php.notes 
Request: Send a blank email to php-notes+get-139633@lists.php.net to get a copy of this message
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)?". "|&iquest;(\s)?|&iexcl;(\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 !="&iquest;" && $prevStr !="&iexcl;"){ $sentencegood =" ".$sentencegood; } $newtext[] = $sentencegood; $prevStr =$text; }elseif($size==1 ){ if ($i>0 && ($text == '¿' || $text == '¡' || $prevStr =="&iquest;" || $prevStr =="&iexcl;") ){ $newtext[] =" ".$text; }else{ $newtext[] =$text; } $prevStr =$text; } } $textgood = implode($newtext); return $textgood; } ---- Server IP: 69.147.83.197 Probable Submitter: 213.27.159.25 ---- Manual Page -- http://www.php.net/manual/en/function.ucwords.php Edit -- https://master.php.net/note/edit/83383 Del: integrated -- https://master.php.net/note/delete/83383/integrated Del: useless -- https://master.php.net/note/delete/83383/useless Del: bad code -- https://master.php.net/note/delete/83383/bad+code Del: spam -- https://master.php.net/note/delete/83383/spam Del: non-english -- https://master.php.net/note/delete/83383/non-english Del: in docs -- https://master.php.net/note/delete/83383/in+docs Del: other reasons-- https://master.php.net/note/delete/83383 Reject -- https://master.php.net/note/reject/83383 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139633) next »