The following php example helps you to strip the special characters and return only what is required and very useful in search queries to avoid XSS injections.
Code Example:
<?php $text = 'this>! is %#4>sample* file 01.jpg<script>'; $output = preg_replace('/[^a-zA-Z\s0-9\.]/', '', $text); echo $output; ?>