Re: sentence case

From: Date: Wed, 23 Feb 2005 22:26:18 +0000
Subject: Re: sentence case
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-209301@lists.php.net to get a copy of this message
Diana Castillo wrote:
with php, HOW CAN I CONVERT SENTENCES LIKE THIS . TO BE UPPER AND LOWER CASE to become like this : How can I convert sentences like this. To be upper and lower case.
here is something I hacked together for Smarty once, modified a bit to suit your need more (syntax-checked only), maybe it gives you some more to go on: function ucsentence($string, $lower = false /* dont think this is a good idea*/) {
    if ((boolean)$lower) {
$string = strtolower($string);
    }
    // find this
    $find = array('/e\.g(\. | )/i', '/i\.e(\. | )/i', '/q\.e\.d(\. | )/i', '/ i /i');
    // replace with this temporarily
    $temp = array('>>::EG::<<.',    '>>::IE::<<.',    '>>::QED::<<.',      '>>::I::<<.');
    // replace temp with this at end of processing
    $repl = array('e.g. ',          'i.e. ',          'q.e.d. ',           ' I ');
    // filter out 'e.g's and other text 'atoms' that contain fullstops that do not
    // denote the end of a sentence.
    $string = preg_replace($find, $temp, $string);
    // find sentences
    $sentences = preg_split("/(\!(\s)?|\.(\s)?|\?(\s)?)/",$string, -1,PREG_SPLIT_DELIM_CAPTURE);
    // build the output string.
    foreach($sentences as $k => $sentence) {
        if (in_array(trim($sentence), array('.','?','!'))) {
            continue;
        }
        $sentences[ $k ] = ucfirst(trim($sentence));
    }
    // put the 'e.g.'s back etc
    $string = str_replace($temp, $repl, join('', $sentences));
    return $string;
}

« previous php.general (#209301) next »