Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP filter using preg_replace to allow only alphanumerics and some punctuation

Tags:

regex

php

I have a little problem with preg_replace.

I need a function that removes all characters except [A-z][0-9] and .!?.

I could use preg_match, but this only verifies the string, and I want to remove the characters.

This is so I don't end up putting junk characters like <p> and ;[[;[p;[ in the description META tag.

So the function must do this:

;")<br>kk23?!brkk23?!

Any help would be appreciated :D

like image 919
Master345 Avatar asked Aug 14 '11 20:08

Master345


5 Answers

$string = ';")<br>kk23?!'; 
$new_string = preg_replace("/[^A-Za-z0-9.!?]/",'',$string);
echo $new_string;

Leaves: letters, numbers, spaces, .!?


/* 3 choices. Pick one you like! */
$str = preg_replace("/[^A-Za-z0-9.!? ]/","",$str);
$str = preg_replace("/[^A-Za-z0-9.!?\s]/","",$str);
$str = preg_replace("/[^A-Za-z0-9.!?[:space:]]/","",$str);
like image 187
dang Avatar answered Nov 10 '22 00:11

dang


 $var=preg_replace('~[^A-Za-z0-9?.!]~','',$var);

Don't forget A-Za-z and A-z are not same

like image 20
RiaD Avatar answered Nov 10 '22 00:11

RiaD


The easiest way is to just do something similar to: Just add the characters after the !, make sure to escape them if needed.

$string = "<br>kk23?!";
$string = preg_replace('/[^A-Za-z0-9 \?!]/', '', $string);
like image 2
Ben Avatar answered Nov 09 '22 23:11

Ben


A quick solution will be as below also:

if (preg_match('/^[\w\.]+$/', $str)) {
    echo 'Str is valid';
} else
    echo 'Str is invalid';

// string only contain the a to z , A to Z, 0 to 9 and _ (underscore)

\w - matches [a-zA-Z0-9_]+

Hope it helps.

like image 2
Aditya P Bhatt Avatar answered Nov 10 '22 00:11

Aditya P Bhatt


More visit to this page. I think more people getting the same problem. The better way is to try your self and get what you need. Custom yours or copy paste this php and try it :

$sample_input = '&&*9?><<script>}cat-<html>ch(_P.,,mE.:;xc##e*p32t.ion $e){di+-($e->ge69tMesPHP _f0sage()3);}';

$output = ereg_replace("[^..........]", "", $sample_input);        

echo "validate =".$output;

modify by filling this to get what you want :

 $output = ereg_replace("[^.........]", "", $sample_input);

Example : if you want only lowercase then do like this :

$output = ereg_replace("[^a-z]", $sample_input);

lower case with white space :

 $output = ereg_replace("[^a-z ]", $sample_input);

and more....., This is a simple validation method :

$username = ereg_replace("[^A-Z0-9_]", "", $username);
$fullname = ereg_replace("[^A-Za-z0-9., ]", "", $fullname);
$city     = ereg_replace("[^A-Za-z -]", "", $city);
$phone    = ereg_replace("[^0-9 +()-]", "", $phone);
$state    = ereg_replace("[^A-Za-z -]", "", $state);
$zipcode  = ereg_replace("[^0-9]", "", $zipcode);
$country  = ereg_replace("[^A-Za-z -]", "", $country);
$gender   = ereg_replace("[^mf]", "", $gender);

Try yourself, hope will help...

like image 1
user3706926 Avatar answered Nov 09 '22 23:11

user3706926