JonnoTheDev Posted November 7, 2008 Share Posted November 7, 2008 Anyone know of a library that can take in a string and auto-format the sentence structure. i.e. Original ========= this is a sample sentence. this is another sample sentence. this is the start of a new paragraph. New ========= This is a sample sentence. This is another sample sentence. This is the start of a new paragraph. Quote Link to comment https://forums.phpfreaks.com/topic/131764-solved-sentence-structures/ Share on other sites More sharing options...
ILMV Posted November 7, 2008 Share Posted November 7, 2008 $string = strtolower($string); $string = substr_replace($string, strtoupper(substr($string, 0, 1)), 0, 1); Quote Link to comment https://forums.phpfreaks.com/topic/131764-solved-sentence-structures/#findComment-684430 Share on other sites More sharing options...
JonnoTheDev Posted November 7, 2008 Author Share Posted November 7, 2008 That will only format the first charater of the text. Needs to be all sentences/paragraphs in the entire string (could be a whole article). Also do not want to convert to lowercase, lets say the text had NASA in it. Dont want it coming out as nasa. Quote Link to comment https://forums.phpfreaks.com/topic/131764-solved-sentence-structures/#findComment-684431 Share on other sites More sharing options...
ILMV Posted November 7, 2008 Share Posted November 7, 2008 No, fair point ... I will continue to look for an example Quote Link to comment https://forums.phpfreaks.com/topic/131764-solved-sentence-structures/#findComment-684435 Share on other sites More sharing options...
ILMV Posted November 7, 2008 Share Posted November 7, 2008 Ok so I had a concept in my mind of which is in this post... http://objectmix.com/php/490027-re-php-sentence-case.html#post1757216 ...It is the long way round, and some may criticise it for being inefficient with high volumes, but it seems t be your only answer. ILMV Quote Link to comment https://forums.phpfreaks.com/topic/131764-solved-sentence-structures/#findComment-684446 Share on other sites More sharing options...
JonnoTheDev Posted November 7, 2008 Author Share Posted November 7, 2008 Thats the theory I cam up with but its a pain in the arse to keep the rest of the text formatted. Quote Link to comment https://forums.phpfreaks.com/topic/131764-solved-sentence-structures/#findComment-684448 Share on other sites More sharing options...
ILMV Posted November 7, 2008 Share Posted November 7, 2008 Although I haven't checked, that shouldn't cause any formatting problems with the rest of your string, NASA will still appear as NASA. Quote Link to comment https://forums.phpfreaks.com/topic/131764-solved-sentence-structures/#findComment-684449 Share on other sites More sharing options...
JonnoTheDev Posted November 7, 2008 Author Share Posted November 7, 2008 Sorted. Heres how I did it if you are interested: $string = "chester is a county city in Cheshire, England, located in what is named the English Northwest. chester lies on the River Dee, a water body that borders Wales."; // explode each paragraph // each empty array value is a line break (carraige return) $paragraphs = preg_split('/\\n{1,}/', $string, -1, PREG_SPLIT_NO_EMPTY); // loop through each paragraph and explode the sentences for($x = 0; $x < count($paragraphs); $x++) { $stringParts = explode(". ", $paragraphs[$x]); // loop through each sentence and capitalize the first letter for($i = 0; $i < count($stringParts); $i++) { $stringParts[$i] = ucfirst($stringParts[$i]); } // implode the string parts back into sentences $paragraphs[$x] = implode(". ", $stringParts); } // build back up the new string foreach($paragraphs as $paragraph) { $newString .= strlen($paragraph) ? $paragraph : "\n"; } print nl2br($newString); Quote Link to comment https://forums.phpfreaks.com/topic/131764-solved-sentence-structures/#findComment-684456 Share on other sites More sharing options...
ILMV Posted November 7, 2008 Share Posted November 7, 2008 *Bookmarked* I can see that coming in handy at some, good to see your problem solved. Quote Link to comment https://forums.phpfreaks.com/topic/131764-solved-sentence-structures/#findComment-684457 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.