PHP regex - Remove special characters from a string


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

This will remove all non-ascii characters / special characters from a string.


Copy this code and paste it in your HTML
  1. //Remove from a single line string
  2. $output = "Likening ‘not-critical’ with";
  3. $output = preg_replace('/[^(\x20-\x7F)]*/','', $output);
  4. echo $output;
  5.  
  6. //Remove from a multi-line string
  7. $output = "Likening ‘not-critical’ with \n Likening ‘not-critical’ with \r Likening ‘not-critical’ with. ' ! -.";
  8. $output = preg_replace('/[^(\x20-\x7F)\x0A\x0D]*/','', $output);
  9. echo $output;

URL: http://daipratt.co.uk/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.