php strip special characters from text using preg_replace


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.