Detect Upper case characters in a string and separate them with a space

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zainally@gmail.com

    Detect Upper case characters in a string and separate them with a space

    I want to detect uppercase characters in a string and separate the two
    words in it by a space. I know I can use preg_replace for it, but I am
    not very good at regular expressions. Can someone help?

    One example of such a string follows. Please note that there are a lot
    of strings like these and I want a generic method to do it, which is
    why I think preg_replace would be the perfect solution. If someone can
    help me, that would be great!

    Example:

    $string = "UserLocati on";

    I want the string to be $string = "User Location";



    Zain Ally

  • Toby A Inkster

    #2
    Re: Detect Upper case characters in a string and separate them with aspace

    zainally wrote:
    I want to detect uppercase characters in a string and separate the two
    words in it by a space. I know I can use preg_replace for it, but I am
    not very good at regular expressions. Can someone help?
    You want something like this:

    $s = preg_replace('/([^\s])([A-Z])/', '\1 \2', $s);

    You probably want to consider what sort of behaviour is "desirable" in
    edge cases like this:

    Print404ErrorPa ge
    GetHTTPRequest
    Jpeg2Gif

    The above regular expression will output:

    Print404 Error Page
    Get H T T P Request
    Jpeg2 Gif

    which might not be what you wanted!

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
    Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

    * = I'm getting there!

    Comment

    • zainally@gmail.com

      #3
      Re: Detect Upper case characters in a string and separate them with a space

      On Mar 19, 5:08 am, Toby A Inkster <usenet200...@t obyinkster.co.u k>
      wrote:
      zainally wrote:
      I want to detect uppercase characters in a string and separate the two
      words in it by a space. I know I can use preg_replace for it, but I am
      not very good at regular expressions. Can someone help?
      >
      You want something like this:
      >
      $s = preg_replace('/([^\s])([A-Z])/', '\1 \2', $s);
      >
      You probably want to consider what sort of behaviour is "desirable" in
      edge cases like this:
      >
      Print404ErrorPa ge
      GetHTTPRequest
      Jpeg2Gif
      >
      The above regular expression will output:
      >
      Print404 Error Page
      Get H T T P Request
      Jpeg2 Gif
      >
      which might not be what you wanted!
      >
      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~http://tobyinkster.co.uk/contact
      Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
      >
      * = I'm getting there!
      That is exactly what I want. Thanks Toby!

      Comment

      • Alan

        #4
        Re: Detect Upper case characters in a string and separate them with a space


        "Toby A Inkster" <usenet200703@t obyinkster.co.u kwrote in message
        news:ms01d4-taf.ln1@ophelia .g5n.co.uk...
        zainally wrote:
        >
        >I want to detect uppercase characters in a string and separate the two
        >words in it by a space. I know I can use preg_replace for it, but I am
        >not very good at regular expressions. Can someone help?
        >
        You want something like this:
        >
        $s = preg_replace('/([^\s])([A-Z])/', '\1 \2', $s);
        >
        You probably want to consider what sort of behaviour is "desirable" in
        edge cases like this:
        >
        Print404ErrorPa ge
        GetHTTPRequest
        Jpeg2Gif
        >
        The above regular expression will output:
        >
        Print404 Error Page
        Get H T T P Request
        Jpeg2 Gif
        >
        which might not be what you wanted!
        >
        Further to Toby's post:

        $TestStr = Print404ErrorPa ge\nGetHTTPRequ est\nJpeg2Gif\n JPEG";

        $CapRegX = '/(\B[A-Z])(?=[a-z])|(?<=[a-z])([A-Z])/sm';
        $RepStr = ' $1$2';
        $OutStr = preg_replace($C apRegX,$RepStr, $TestStr);

        produces:

        Print404 Error Page
        Get HTTP Request
        Jpeg2 Gif
        JPEG

        HTH
        --

        Alan


        Comment

        Working...