Posted By

daipratt on 12/27/10


Tagged

php


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

mikael12


PHP regex - Remove special characters from a string


 / Published in: PHP
 

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

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

  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;

Report this snippet  

You need to login to post a comment.