SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Uppercasing in php

    I need to "clean" a sentence. Basically it needs to be all lower case except the first word in the sentence. ucfirst does the first word, but if you have a second sentence how do you upper case it after the period?

    $string = 'this is a test. that is a test';

    $string_uc = ucfirst($string);

    so $string_uc would look like:

    This is a test. that is a test

    I still need to upper case that to That. Do I use a preg_match? Please help

    Silly

  2. #2
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could try something like this:
    PHP Code:
    $string_array explode(". " $full_string); 
    // breaks the entire string up into chunks based on the periods

    $fixed_full_string "";
    foreach (
    $string_array as $piece) {
       
    $piece_uc ucfirst($piece);
       
       
    $fixed_full_string .= $piece_uc .'. '// adds the period and a space on the end of each piece



  3. #3
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wrote this script to upper after ? or . or !. But you can easily add what you need:

    PHP Code:
    function properSentence($var)
    {

    $var2 ucwords($var[1]);

    return 
    $var2;

    }

    $desc preg_replace_callback('#((\?|\.|!) [a-z]+)#',"properSentence",$desc); 
    Silly

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are normally one or more spaces after punctuator... And you need to replace only one chracter...

    PHP Code:
    preg_replace("~[.!?]\s*[a-z]~se""strtoupper('$0')"$text); 

  5. #5
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you explain this part of your code?

    ('$0')

    Does that replace the first occurance found in the preg match?

    Edit: The e specified in ~se, is that what tells the ('$0') to evaluate as php code? So it would change the first character found in preg_replace?

    Silly

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $0 means 'whole pattern'.

    Assume you have "....! foo..." anywhere in search string. Regexp matches "! f" and assigns $0 = "! f". Then this is substituted in replace string, giving "strtoupper('! f')". This string is evaluated (because of 'e'), producing "! F". Finally, preg makes a replacement, and result is "....! Foo....", what is exactly what we need.

  7. #7
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So does it have to be $0? Or anything I assign it? Sounds like it uses php eval() function and assign the match to whatever variable is in ('$0')

    Silly


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •