Skip to content

Instantly share code, notes, and snippets.

@justinhernandez
Forked from delonnewman/escape_mb_chars.php
Created November 10, 2016 06:41
Show Gist options
  • Save justinhernandez/d4a273378c50ceca18f54b58f5c0586f to your computer and use it in GitHub Desktop.
Save justinhernandez/d4a273378c50ceca18f54b58f5c0586f to your computer and use it in GitHub Desktop.
<?php
$testString = "• janky • stuff • take me away ••";
function escape_mb_chars($str) {
$first = preg_replace_callback('/\p{C}/u', function ($m) {
$char = current($m);
return substr(json_encode($char), 1, -1);
}, $str);
return rtrim($first, '\n');
}
function clean_string($str) {
return preg_replace('/[^a-zA-Z0-9\s]/', '', $str);
}
print clean_string($testString) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment