SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Reg-Ex Stuff

  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    Chicago, USA
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reg-Ex Stuff

    Hello,

    Say I have a string that contains "add_product". What would be the regular expression for changing such a string into AddProductAction? In other words, I want to get rid of the underscores and capitalize the first letter of each word.

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    Toronto
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check out
    str_replace()

    str_replace("add_product", "AddProductAction", $string);

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    Chicago, USA
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I should've been more specific. "add_product" was just an example string. How about if the string was "edit_product" or "list_products"? I want to be able to change those last two to "EditProductAction" and "ListProductsAction".

  4. #4
    . 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:
    $data 'add_product';
    $data str_replace(' '''ucwords(str_replace('_'' '$data))) . 'Action'
    I'm sure there is a better way kinda just hacked it to gather...but it is a start i suppose.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd say
    PHP Code:
    $camel_text preg_replace(
        
    '/([a-z]+)_([a-z]+)/e',
        
    'ucfirst("$1").ucfirst("$2")."Action"',
        
    $underscores_text); 


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
  •