1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

strip special characters from variable

Discussion in 'PHP' started by Joobz, Jan 15, 2008.

  1. #1
    I have a variable passed from a form via POST and want to know how to strip special characters from it before passing it to the mysql query. If anyone knows the function, please post.
     
    Joobz, Jan 15, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    php.net/mysql_real_escape_string

    Example#1 Simple mysql_real_escape_string() example
    
    <?php
    // Connect
    $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
        OR die(mysql_error());
    
    // Query
    $query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
                mysql_real_escape_string($user),
                mysql_real_escape_string($password));
    ?>
    
    PHP:
     
    jayshah, Jan 15, 2008 IP
  3. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I had hoped to remove special characters prior to passing the variable to the mysql query.
    I tried str_replace but that still doesn't remove apostrophes or something that is part of php even when I tired to escape them with a backslash.
     
    Joobz, Jan 15, 2008 IP
  4. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Do this:

    
    function cleanse($string)
    {
            $test = str_replace('"', '\"', strip_tags($string));
            $test = escapeshellcmd($string);
            #the above statement should remove the ' but just to be on the safe side
            $test = ereg_replace("'", "", $string);
            return($test);
    }
    
    Code (markup):
     
    LittleJonSupportSite, Jan 15, 2008 IP
  5. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The only way that worked is when I took everything out and just used:

    
     $string = ereg_replace("'", "", $string);
    
    PHP:
    was I wrong in not declaring a function?
     
    Joobz, Jan 15, 2008 IP
  6. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #6
    $string = preg_replace("/[^a-zA-Z0-9s]/", "", $string);
    Code (markup):
     
    papa_face, Jan 15, 2008 IP
  7. thapame

    thapame Well-Known Member

    Messages:
    739
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    180
  8. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #8
    could you please tell me what the regex /[^a-zA-Z0-9s]/ means?

    I know a little but am unsure about what this accomplishes.
    Thanks.
     
    Joobz, Jan 15, 2008 IP
  9. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #9
    Basically it checks a variable for a certain condition. In this case regex is used to strip everything except alphanumeric characters and then stores the output back into $string.
     
    papa_face, Jan 15, 2008 IP
  10. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #10
    How do I use this exact regex but also allowing hyphens? (this is for domain names).
     
    Joobz, Jan 15, 2008 IP
  11. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #11
    regex isn't my forté, try searching on google for domain name validation.
     
    papa_face, Jan 15, 2008 IP
  12. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thanks for your help on this. Not sure what this means but I will try it:
    /^[a-z0-9][a-z0-9\-]+[a-z0-9]$/i

    I found it here:
    domain validation

    if anyone knows how to decode this into english, please post.
     
    Joobz, Jan 15, 2008 IP
  13. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #13
    Its the other one on that page that I think you need btw.
     
    papa_face, Jan 15, 2008 IP
  14. Joobz

    Joobz Peon

    Messages:
    598
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #14
    yep, it's the second one ... I'm just wondering what the heck the "/i" represents at the end.
     
    Joobz, Jan 15, 2008 IP
  15. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #15
    This matches a string with starts with one letter or number, then is followed by any number (>=1) of letters, numbers or hyphens, then ends with a letter or number. The /i makes it case insensitive, so the letters can be upper-case even though the regex was a-z and not a-zA-Z.

    So this would allow things like:

    abcde
    ab95
    a------------------------bb----bbbbb--f-gd-gb---3
    3fe

    but not allow:

    -9df
    ab
    9
    a.b

    As for your actual goal here, if you just want to safely store the string in the database, then mysql_real_escape_string() as initially suggested by jayshah is the correct answer. If, however, there is some other reason why you don't want any other characters in it, then indeed you need to pursue the regex business.
     
    SmallPotatoes, Jan 16, 2008 IP