Posts: 1,832
Location: Somewhere else entirely
|
This regex ought to do it:
PHP Code:
$username = $_POST['username'];
$username= preg_replace("#[^A-Za-z0-9]#","",$username);
Brief explanation:
Preg_replace looks for things that match the regex, replaces them with the second string argument (in this case "" the empty string). The string it does the replacement on is the third argument, in this case our username.
The regex includes a single character class denoted by the square brackets. It includes only letters and numbers. The first character in the class is '^' which when placed at the start will invert the character class (so it matches everything except letters and numbers).
So you have a regex that matches everything but letters and numbers, and then replaces them with "", leaving you with just letters and numbers in the original username. If you want to allow certain characters like _, you just add them between the [] as long as the ^ stays at the start, e.g #[^A-Za-z0-9_]#
The #s just mark the start and end of the regex.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Please login or register to view this content. Registration is FREE (aka MSN handwriting for forums)
|