Return to Snippet

Revision: 33309
at October 7, 2010 20:47 by blipz


Updated Code
# Legend for regular expression pattern used
# \s - whitespace
# [] - grouping
# \ - escape character
# \. - dot character


$text = 'this>! is %#4>sample* file 01.jpg';
$output = preg_replace('/[^a-zA-Z\s0-9\.]/', '', $text);
echo $output;


#result: this is 4sample file 01.jpg

Revision: 33308
at October 7, 2010 20:45 by blipz


Initial Code
$text = 'this>! is %#4>sample* file 01.jpg';
$output = preg_replace('/[^a-zA-Z\s0-9\.]/', '', $text);
echo $output;

result: this is 4sample file 01.jpg

Initial URL


Initial Description
This will strip special characters and return only what is required and very useful in search queries to avoid xss injections.

Initial Title
php strip special characters from text using preg_replace

Initial Tags


Initial Language
PHP