I was hoping someone could point me in the right direction. I need to have the ability to format stories that people post. Lets say their story is submitted with all capital letters. Can I use strtolower and then scan the story for punctuation allowing me to capitalize the first letter of the following word? Any suggestions would be appreciated.
[Resolved] Capitalize after punctuation?
try this...
function FixTextCase($text) {
$text = strtolower($text);
$sentences = preg_split("/(\.(\s)?|\?(\s)?)/",$text,-1,PREG_SPLIT_DELIM_CAPTURE);
foreach($sentences as $x) {
if ((trim($x) == ".") || (trim($x) == "?")) {
$outtext .= trim($x)." ";
} else {
$outtext .= ucfirst(trim($x));
}
}
// fix double mark errors
$outtext = preg_replace("/(\s\.)/",".",$outtext);
$outtext = preg_replace("/(\s\?)/","?",$outtext);
return $outtext;
}
<?php
function capitalize_story($story) {
//list everything that could end a sentence.
$punctuation = array('.','?','!');
foreach($punctuation as $ender) {
//turn the story into an array of sentences that end
//with the current punctuation
$temp = explode($ender,$story);
//empty story so we can rebuild it
$story = '';
//capitalize the first letter of each sentence.
foreach($temp as $sentence) {
$sentence = ltrim($sentence);
$sentence = ucfirst($sentence);
$story .= $sentence . ' ';
} //end foreach
} //end foreach
return $story;
} //end capitalize_story
?>
WARNING: This will effectively squash paragraphs you'll have to figure that one out on your own.
Thanks for the quick response (both of you)
drawmack
That did work, but it didnt replace the punctuation.
Is it possible to find the space after the "$ender" and use IT as the seperator?
<?php
function capitalize_story($story) {
//list everything that could end a sentence.
$punctuation = array('.','?','!');
foreach($punctuation as $ender) {
//turn the story into an array of sentences that end
//with the current punctuation
$temp = explode($ender,$story);
//empty story so we can rebuild it
$story = '';
//capitalize the first letter of each sentence.
foreach($temp as $sentence) {
$sentence = ltrim($sentence);
$sentence = ucfirst($sentence);
$story .= $sentence . $ender . ' ';
} //end foreach
} //end foreach
return $story;
} //end capitalize_story
?>
okay I fixed it so it puts the puncutation back now. As far as using the space after punctuation. If you don't trust them to capitalize the first word of a sentence do you really trust them to put a space in?
Perfect. I appreciate your help with this.