#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0

    String manipulation help


    I need a way to take a string "ThisString" find the occurrences of any uppercase letters and add a space " " between the last lowercase letter and the uppercase letter. IE:
    "ThisString" to "This String"
    or
    "AnotherStringHere" to "Another String Here"

    any help would be appreciated.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,069
    Rep Power
    9398
    Regular expressions.
    PHP Code:
    $new preg_replace('/\B[A-Z]/'' $1'$old); 
    Finds a capital letter in the middle of a "word" and sticks a space in front of it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    That actually replaced the uppercase with a blank. It didn't put a space in front of the uppercase. But at least it did do it only where there was an uppercase.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,069
    Rep Power
    9398
    I changed my mind a couple times before posting what I did. It's kinda a hybrid.
    PHP Code:
    $new preg_replace('/\B[A-Z]/'' $0'$old); 
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    8
    Rep Power
    0
    Way to kewl!

    You should have seen some of the gyrations that I was trying to do to manipulate the string. Nothing was working, and wham, you give me a quick (short) single line that did it!

    Thank you so very much.

IMN logo majestic logo threadwatch logo seochat tools logo