www.webdeveloper.com
Results 1 to 6 of 6

Thread: Convert wholly capitalized words in a string into uppercase first only

  1. #1
    Join Date
    Dec 2011
    Posts
    28

    Convert wholly capitalized words in a string into uppercase first only

    Hi all,

    I have a string in which I want to find any wholly uppercase word (e.g. TEST ) and then convert it into ( Test ) .

    I can't code it , headache after many trials !

    Can someone help ?

    Hint : I can't use explode(" ",$string ) to convert it into an array to itreate it .. Because the string words are not only separated by spaces , but some words are separated by \n

    Regards,
    Dr Mostafa

  2. #2
    Join Date
    Feb 2012
    Location
    youTUBE
    Posts
    234
    Given a string S1, replace "\n", using str_replace(), with a special token " xx--99---2 ", where the special token has both a leading and trailing blank, to give string S2. Now explode S2, and do your operation on the exploded result. but be careful that the operations done will not affect the special token. Subsequently implode the new results to give the string S3. Finally replace the special token using str_replace(), with "\n" to give S4, and your task is complete.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,518
    ucwords()

    (Any time you want to manipulate a string, it's always a good idea to start here first. )

    PS: If for some reason you wanted to go the explode() route, instead use preg_split().
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Apr 2010
    Posts
    88
    You could use preg_replace_callback to replace only the wholly uppercase words.

    $string = preg_replace_callback('#\b([A-Z]+)\b#s', function($m) { return ucwords(strtolower($m[1])); }, $string);

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,518
    Quote Originally Posted by gvre View Post
    You could use preg_replace_callback to replace only the wholly uppercase words.

    $string = preg_replace_callback('#\b([A-Z]+)\b#s', function($m) { return ucwords(strtolower($m[1])); }, $string);
    Hmm...think I missed the part about only all-caps words. That looks like a good solution, though there might be some edge cases that could be problematic, such as the name "MACTAVISH", which should normally be spelled "MacTavish". (And we won't even try to figure out to do with "Dupont", "DuPont", "duPont", "Du Pont", or "du Pont". )
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Apr 2010
    Posts
    88
    You are right but it is not difficult to implement some words' exceptions/fixes to the code.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles