After following the lead from https://stackoverflow.com/users/395384/epb I used json_decode to translate the unicode which works.

$unicode = json_decode("\u00fc\u00be\u008c\u00a3\u00a4\u00bc") ;
$column = str_replace($unicode, "'", urldecode(columnIndex]));
Answer from Keith John Hutchison on Stack Overflow
🌐
PHP
php.net › manual › en › function.mb-substitute-character.php
PHP: mb_substitute_character - Manual
If substitute_character is not set, it returns the current setting. ... <?php /* Set with Unicode U+3013 (GETA MARK) */ mb_substitute_character(0x3013); /* Set hex format */ mb_substitute_character("long"); /* Display current setting */ echo mb_substitute_character(); ?>
🌐
GitHub
gist.github.com › niquenen › d06a55ddf11f4a08a421750c2ccb96b6
PHP function to replace all characters with an ASCII equivalent. · GitHub
Learn more about bidirectional Unicode characters ... I redirect to this post on Stack Overflow. An example of the function can be found below (or on PHPSandbox): <?php $array = array( 'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a
🌐
Stack Overflow
stackoverflow.com › questions › 40834347
php - Replace unicode characters in filename? - Stack Overflow
How can I replace all unicode characters like that with their corresponding special chars? ... That is a JSON encoded array. Use the json_decode function to convert it to a PHP array.
🌐
SitePoint
sitepoint.com › php
How to represent a unicode character in php source code - PHP - SitePoint Forums | Web Development & Design Community
November 5, 2007 - i’d like to do this: str_replace( ‘\’', ‘’’, $string ); where the red replace string is/should be a right single quote. how to include/represent that character in the above situation? with the above the right single quote gets mangled up (in the source code/text editor itself that is). so i tried: str_replace( ‘\’', 0xE28099, $string ); as 0xE28099 is the right single quote in utf-8, but that didnt’ work. any ideas? thanks.
Top answer
1 of 2
6

From the PHP manual:

Single and double quoted PHP strings have special meaning of backslash. Thus if \ has to be matched with a regular expression \\, then "\\\\" or '\\\\' must be used in PHP code.

First of all, in your regular expression, you're only using one backslash (\). As explained in the PHP manual, you need to use \\\\ to match a literal backslash (with some exceptions).

Second, you are missing the capturing groups in your original expression. preg_replace() searches the given string for matches to the supplied pattern and returns the string where the contents matched by the capturing groups are replaced with the replacement string.

The updated regular expression with proper escaping and correct capturing groups would look like:

$str2 = preg_replace('/\\\\u([0-9a-f]+)/i', '&#x$1;', $str);

Output:

&#x304a;&#x306f;&#x3088;&#x3046;

Expression: \\\\u([0-9a-f]+)

  • \\\\ - matches a literal backslash
  • u - matches the literal u character
  • ( - beginning of the capturing group
    • [0-9a-f] - character class -- matches a digit (0 - 9) or an alphabet (from a - f) one or more times
  • ) - end of capturing group
  • i modifier - used for case-insensitive matching

Replacement: &#x$1

  • & - literal ampersand character (&)
  • # - literal pound character (#)
  • x - literal character x
  • $1 - contents of the first capturing group -- in this case, the strings of the form 304a etc.

RegExr Demo.

2 of 2
1

This page here—titled Escaping Unicode Characters to HTML Entities in PHP—seems to tackle it with this nice function:

function unicode_escape_sequences($str){
  $working = json_encode($str);
  $working = preg_replace('/\\\u([0-9a-z]{4})/', '&#x$1;', $working);
  return json_decode($working);
}

That seems to work with json_encode and json_decode to take pure UTF-8 and convert it into Unicode. Very nice technique. But for your example, this would work.

$str = '\u304a\u306f\u3088\u3046';
echo preg_replace('/\\\u([0-9a-z]{4})/', '&#x$1;', $str);

The output is:

&#x304a;&#x306f;&#x3088;&#x3046;

Which is:

おはよう

Which translates to:

Good morning

Find elsewhere
🌐
GitHub
gist.github.com › ahmadazimi › b1f1b8f626d73728f7aa
PHP replace Zero Width Space using preg_replace · GitHub
... Does not removes U+FEFF, if you use exact range shown in sample code. You need to include it explicitly, out of character class. $text = preg_replace( '/[\x{200B}-\x{200D}\x{FEFF}]/u', '', $text );
🌐
Stack Overflow
stackoverflow.com › questions › 48332803 › preg-replace-unicode-characters
php - preg_replace unicode characters - Stack Overflow
I've tried several preg_match solutions for the first bit, but it either doesn't remove any characters from the string, or removes everything. Below is the latest attempt, /(^\\\u[0-9a-f]{4})+/ Not being too familiar with Regex, I'm starting to scratch my head in confusion as I'm not really sure what else to try. This is so that eventually, I'm able to insert each unicode into a database as its own record. php · unicode · preg-replace ·
🌐
Alvin Alexander
alvinalexander.com › php › how-to-remove-non-printable-characters-in-string-regex
PHP: How to remove non-printable characters from strings | alvinalexander.com
July 14, 2019 - If you do need to handle Unicode characters, this SO page shows a possible solution. Finally, while I’m in the neighborhood, here’s a list of PHP “range” regular expressions from the php.net regex page.