SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 ) using php .

    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
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Try ucwords, example from PHP manual
    PHP Code:
    <?php
    $foo 
    'hello world!';
    $foo ucwords($foo);             // Hello World!

    $bar 'HELLO WORLD!';
    $bar ucwords($bar);             // HELLO WORLD!
    $bar ucwords(strtolower($bar)); // Hello World!
    ?>

  3. #3
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You will need to use preg_replace_callback.

    Something like this (warning: hasn't been tested).
    PHP Code:
    $string "The quick BROWN fox jumped over the LAZY dog.";


    $new_string preg_replace_callback('~[A-Z]{2,}~'create_function('$m''return ucwords(strtolower($m[0]));'), $string); 


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
  •