Results 1 to 4 of 4

Thread: ucwords, pregreplace, skip first

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default ucwords, pregreplace, skip first

    I have this code:

    PHP Code:
    $title trim($_POST['title']);
    $case_changed ucwords($title);
    $case_changed str_replace("Of""of"$case_changed);
    $case_changed str_replace("And""and"$case_changed);
    $case_changed str_replace("For""for"$case_changed);
    $case_changed str_replace("From""from"$case_changed);
    //Multiple patterns and replacements for entities and greek words
    $case_changed preg_replace($patterns$replacements$case_changed);
    echo 
    $case_changed
    which is suppose to create Title Case for text entered. How can I run the str_replace on all words after the first because the first word should always be capitalized. I was thinking substr($case_changed, 0, $X) but the first word will never have the same amount of characters.

    Thanks for any ideas. (I solved my second question about ucwords and pregreplace after posting and seeing the code some other place).
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    This is all about ordering.
    First, uppercase all words; second, lowercase the specific words you want to avoid; third, make the first word of the string uppercase:
    $str[0] = strtoupper($str[0]);

    Another way to go about this would be to explode everything into words and do this individually to each word, that way giving individual control for each word. But the way above works.

    You can also setup an array to do the re-lowercasing.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    I forgot about this one, I came up with this...

    PHP Code:
    $title trim($_POST['title']);
    $case_changed ucwords($title);
    $case_changed str_replace("Of ""of "$case_changed);
    $case_changed str_replace("The ""the  "$case_changed);
    $case_changed str_replace("From""from"$case_changed);
    $title_length strlen($case_changed);
    $case_changed strtoupper(substr($case_changed01)) . substr($case_changed1$title_length); 
    See anything wrong with it?
    Corrections to my coding/thoughts welcome.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That looks fine. The only problem is that str_replace isn't smart-- it doesn't know when it's a separate word. You have a space after 'of' and 'the' but not 'from'. Still, though, you can find it if it's the end of a word, like 'bathe' for 'the', etc.
    The rest of the logic is fine, but if you find a way to replace 'whole words' that may be better. Regex could be the answer: the word must be bordered by spaces, punctuation or the ends of the string.
    There also may be a PHP function for this, but I don't remember.... take a look around php.net.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    bluewalrus (06-01-2010)

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
  •