Remove Bad Characters PHP/MySql

Discussion in 'MySQL' started by donteatchicken, May 5, 2006.

  1. #1
    I need to strip all the non alpha numerics characters in a string.

    Assume $var = a current mysql query of a field that has garbage like

    b&^$A)(&$\?<>#D#@%!^/T(%$)E&@X(@T

     
    
    $goodtext= (preg_replace(WHAT EXACTLY GOES HERE??);
    
    
    
    PHP:
    I don't even know if it's preg_replace I should use, I want to remove everything thats not a number or a letter.

    TIA
     
    donteatchicken, May 5, 2006 IP
  2. jprice259

    jprice259 Peon

    Messages:
    100
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php

    $string = "Here! is some text, and numbers 12345, and symbols !£$%^&";

    $new_string = preg_replace("/[^a-zA-Z0-9s]/", "", $string);

    echo $new_string

    ?>

    The code should return this:

    Here is some text and numbers 12345 and symbols

    Remove the S

    to take out white space...

    Use that buddy...
     
    jprice259, Apr 21, 2008 IP
    donteatchicken likes this.
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    jprice, you missed a backslash (\) before the "s" (or the forum software stripped it).

    Here is the corrected code:
    $new_string = preg_replace('/[^a-z0-9\s]/i', '', $string);
    Code (markup):
    As jprice meant to say, remove the "\s" if you want it to be strictly alphanumeric (no whitespace allowed)
     
    krt, Apr 21, 2008 IP