Posted By

blipz on 10/07/10


Tagged

strip char special


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jplr


php strip special characters from text using preg_replace


 / Published in: PHP
 

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

  1. # Legend for regular expression pattern used
  2. # \s - whitespace
  3. # [] - grouping
  4. # \ - escape character
  5. # \. - dot character
  6.  
  7.  
  8. $text = 'this>! is %#4>sample* file 01.jpg';
  9. $output = preg_replace('/[^a-zA-Z\s0-9\.]/', '', $text);
  10. echo $output;
  11.  
  12.  
  13. #result: this is 4sample file 01.jpg

Report this snippet  

You need to login to post a comment.