What to replace for a safe url?

Discussion in 'PHP' started by Python, Jul 11, 2007.

  1. #1
    I have a user registration script where they enter their username and other details. I am going to have their profiles accessible at domain.com/username

    What do I need to do to the username to make it safe for the url? I know I need to use str_replace() and I know how to - I just want to know what characters I need to strip out.

    Thanks
     
    Python, Jul 11, 2007 IP
  2. kajol

    kajol Well-Known Member

    Messages:
    523
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    ^\'";&+.................
     
    kajol, Jul 11, 2007 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    If I were doing it I would strip all whitespace, and anything except letters and numbers. You can technically have -'s and _'s in there if you want.

    This should work:

    
    preg_replace("/[^a-zA-Z0-9 ]/","",$string);
    
    //if you want dashes and underscores **Not sure if you need to escape - or _
    preg_replace("/[^a-zA-Z0-9\-\_ ]/","",$string);
    
    PHP:
     
    jestep, Jul 11, 2007 IP
  4. Python

    Python Well-Known Member

    Messages:
    680
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Thanks jestep :D

    Works like a charm.
     
    Python, Jul 11, 2007 IP