Re: sentence case

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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;
}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux