Replace string

Discussion in 'PHP' started by cris02, Feb 12, 2007.

  1. #1
    Hello,

    is there a way to detect the spaces and symbols like @#!~$^&*()+=, etc.. of filename being uploaded and replace it with underscore? any idea? anyone can help? thanks.
     
    cris02, Feb 12, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $name = preg_replace('/[^\w\-\.]/', '_', $name);
    
    PHP:
    Replaces anything that isn't alphanumeric, or a dot, or a score with an underscore.
     
    nico_swd, Feb 12, 2007 IP
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It would be easier to just replace all non-alphanumeric characters. Is that an option for you?
     
    T0PS3O, Feb 12, 2007 IP
  4. cris02

    cris02 Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    yes, and thanks for the simple code snippet it's works!
     
    cris02, Feb 12, 2007 IP