Results 1 to 4 of 4

Thread: How to prepend a hyphen to uppercase characters in a string

  1. #1
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to prepend a hyphen to uppercase characters in a string

    I wondered if there was a short way to place a hyphen before every capital letter in a string except the first capital?

    If string is OneTwoThree I need it to read One-Two-Three

    Tnx

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There may or may not be a shortcut method, and/or a more efficient way of putting together existing methods, but this works:

    PHP Code:
    <?php
    $string 
    'OneTwoThree';
    $shortstrings = array(substr($string01), substr($string1));
    $shortstrings[1] = preg_replace("/([A-Z])/""-$1"$shortstrings[1]);
    $newstring implode(''$shortstrings);
    echo 
    $newstring;
    ?>
    If you're turning it into a function, you should test to make sure $string is a string and has a strlen of at least 2. Like:

    PHP Code:
    <?php
    $mystring 
    'OneTwoThree';
    function 
    hypenatecaps($string){
        if(!
    is_string($string)){return;}
        if(
    strlen($string) < 2){
            return 
    $string;
        }
        
    $shortstrings = array(substr($string01), substr($string1));
        
    $shortstrings[1] = preg_replace("/([A-Z])/""-$1"$shortstrings[1]);
        
    $newstring implode(''$shortstrings);
        return 
    $newstring;
    }
    echo 
    hypenatecaps($mystring);
    ?>
    This also works:

    PHP Code:
    <?php
    $string 
    'OneTwoThree';
    echo 
    substr($string01) . preg_replace("/([A-Z])/""-$1"substr($string1));
    ?>
    Last edited by jscheuer1; 01-09-2013 at 02:03 PM. Reason: add function method and shorter method
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks John!

    I'm just splitting file names and also found this works nice.

    function splitAtUpperCase($s) {

    return preg_replace(/(?<!^)([A-Z])/', '-\\1', $s);
    }
    echo splitAtUpperCase($s);

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Please don't link to other sites unnecessarily.

    Quote Originally Posted by AppsRU View Post
    return preg_replace(/(?<!^)([A-Z])/', '-\\1', $s);
    Will not work because of the non-standard quote (red in the above). But it's a good concept. And using a standard quote fixes it:

    Code:
    return preg_replace('/(?<!^)([A-Z])/', '-\\1', $s);
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Create string with random characters
    By borris83 in forum PHP
    Replies: 11
    Last Post: 03-31-2009, 07:05 PM
  2. convert string to uppercase value
    By boogyman in forum JavaScript
    Replies: 2
    Last Post: 07-04-2008, 01:02 AM
  3. Detect Characters in String
    By T Horton in forum JavaScript
    Replies: 1
    Last Post: 05-18-2008, 01:23 AM
  4. how can I prepend a string?
    By james438 in forum PHP
    Replies: 11
    Last Post: 02-25-2008, 04:09 AM
  5. How to find certain characters in a string
    By Moranor in forum JavaScript
    Replies: 1
    Last Post: 02-09-2007, 10:59 PM

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
  •