SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    Bowen Mountain, NSW
    Posts
    490
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    possibly a preg_match issue??

    G'day to all,

    I am trying to work on a solution to this problem and I am getting all mixed up with my php functions.

    I use camelCasing for my file names and I would like to output the file names as readable text.

    E.G.
    "contactUs"

    I would like to have it as:
    "Contact Us"

    What expression or function would I need to allow this function to work? With the forsight of different file names,

    E.G.
    pageTypes => Page Types

    Many thanks in advance
    Regards,
    BJ Duncan

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    $s 
    'pageTypes';
    $s preg_split'/([A-Z][^A-Z]+)/'$s, -1);

    echo 
    ucwordsjoin$s' ' ) );
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    Bowen Mountain, NSW
    Posts
    490
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for your assistance logic_earth

    Works like a charm. I think I was getting my functions all mixed up. I was trying to use preg_match but was getting myself all mixed up.

    Thank you.
    Regards,
    BJ Duncan

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    An alternative would be to match Capitalized words and prefix them with a space (then make sure the first letter of the string is also uppercase).

    PHP Code:
    <?php

    $s 
    'pageTypes';
    echo 
    ucfirst(preg_replace('/[A-Z][^A-Z]*+/'' $0'$s));
    Salathe
    Software Developer and PHP Manual Author.


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
  •