Small preg_match Help

Discussion in 'PHP' started by farhang, Dec 29, 2007.

  1. #1
    Hello,

    I have this script where it checks the name of a folder, and only allows if it is in format of "something" or "blah blah"

    
    preg_match ( '#[^a-z0-9_\s]|\s{2,}#i', $folder_name )
    
    Code (markup):
    Can anyone help me turn that into something which only allows folder with a "-" instead of space ? or which replaces space with a "-"

    I tried looking through http://www.php.net/preg_match but got stuck...

    Thanks
     
    farhang, Dec 29, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    In your pattern, replace \s (both) with the dash.
     
    nico_swd, Dec 29, 2007 IP
  3. farhang

    farhang Active Member

    Messages:
    383
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Thanks a lot :)

    Is there any quick way to automatically replace space with dash ?
     
    farhang, Dec 29, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    
    $folder_name = str_replace(' ', '-', $folder_name);
    
    PHP:
     
    nico_swd, Dec 29, 2007 IP
  5. farhang

    farhang Active Member

    Messages:
    383
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Wonderful,

    Many thanks again :)
     
    farhang, Dec 29, 2007 IP