My files contain UTF-8. I want to convert UTF-8 characters in the text to \x sequences and preserve everything else.
For example, the text
# Printing 'naïve' and '男孩' with newlines and tabs
print 'naïve\n'
print '\t男孩'
should be converted to
# Printing 'na\xc3\xafve' and '\xe7\x94\xb7\xe5\xad\xa9' with newlines and tabs
print 'na\xc3\xafve\n'
print '\t\xe7\x94\xb7\xe5\xad\xa9'
I tried the following approach:
mb_convert_encoding($text, "UTF-8", "ASCII");
This resulted in
# Printing 'naïve' and 'ç·å©' with newlines and tabs
print 'naïve\n'
print '\tç·å©'
(not quite what I was looking for)
I also tried:
iconv("UTF-8", "ASCII", $text);
This resulted in the error:
PHP Notice: iconv(): Detected an illegal character in input string
How do I get around this?