Skip to content

Instantly share code, notes, and snippets.

@erlangparasu
Created February 12, 2020 20:47
Show Gist options
  • Select an option

  • Save erlangparasu/2fcb5771ddc7d06854b9ab6b0f7aa0b4 to your computer and use it in GitHub Desktop.

Select an option

Save erlangparasu/2fcb5771ddc7d06854b9ab6b0f7aa0b4 to your computer and use it in GitHub Desktop.
To strip all non-ASCII characters from the input string
<?php
// To strip all non-ASCII characters from the input string
$result = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
// That code removes any characters in the hex ranges 0-31 and 128-255, leaving only the hex characters 32-127 in the resulting string, which I call $result in this example.
// Thanks to https://stackoverflow.com/users/6589057/junaid-masood
// Link: https://stackoverflow.com/questions/1176904/php-how-to-remove-all-non-printable-characters-in-a-string/54072059#54072059
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment