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
<?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...
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)