<p>(<strong>Updated a little</strong>)</p>

<p>I'm not very experienced with internationalization using PHP, it must be said, and a deal of searching didn't really provide the answers I was looking for.</p>

<p>I'm in need of working out a reliable way to convert only 'relevant' text to Unicode to send in an SMS message, using PHP (just temporarily, whilst service is rewritten using C#) - obviously, messages sent at the moment are sent as plain text.</p>

<p>I could conceivably convert everything to the Unicode charset (as opposed to using the standard GSM charset), but that would mean that <em>all</em> messages would be limited to 70 characters (instead of 160).</p>

<p>So, I guess my real question is: <em>what is the most reliable way to detect the requirement for a message to be Unicode-encoded, so I only have to do it when it's</em> <strong><em>absolutely necessary</em></strong> <em>(e.g. for non-Latin-language characters)?</em></p>

<h2>Added Info:</h2>

<p>Okay, so I've spent the morning working on this, and I'm still no further on than when I started (certainly due to my complete lack of competency when it comes to charset conversion).  So here's the revised scenario:</p>

<p>I have text SMS messages coming from an external source, this external source provides the responses to me in plain text + Unicode slash-escaped characters.  E.g. the 'displayed' text:</p>

<blockquote>
  <p>Let's test öäü éàè אין תמיכה בעברית</p>
</blockquote>

<p>Returns:</p>

<blockquote>
  <p>Let's test \u00f6\u00e4\u00fc \u00e9\u00e0\u00e8 \u05d0\u05d9\u05df \u05ea\u05de\u05d9\u05db\u05d4 \u05d1\u05e2\u05d1\u05e8\u05d9\u05ea</p>
</blockquote>

<p>Now, I can send on to my SMS provider in plaintext, GSM 03.38 or Unicode.  Obviously, sending the above as plaintext results in a lot of missing characters (they're replaced by spaces by my provider) - I need to adopt relating to what content there is.  What I want to <em>do</em> with this is the following:</p>

<ol>
<li><p>If all text is within the <a href="http://www.dreamfabric.com/sms/default_alphabet.html" rel="nofollow noreferrer">GSM 03.38 codepage</a>, send it as-is.  (All but the Hebrew characters above fit into this category, but need to be converted.)</p></li>
<li><p>Otherwise, convert it to Unicode, and send it over multiple messages (as the Unicode limit is 70 chars not 160 for an SMS).</p></li>
</ol>

<p>As I said above, I'm stumped on doing this in PHP (C# wasn't much of an issue due to some simple conversion functions built-in), but it's quite probable I'm just missing the obvious, here.  I couldn't find any pre-made conversion classes for 7-bit encoding in PHP, either - and my attempts to convert the string myself and send it on seemed futile.</p>

<p><strong>Any help would be greatly appreciated.</strong></p>

[{'answer_id': 27603, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': 0, 'selected': False, 'text': '<p>PHP6 will have better unicode support but there are a few functions you can use.</p>\n\n<p>My first thought was <a href="http://php.net/manual/en/function.mb-convert-encoding.php" rel="nofollow noreferrer"><code>mb_convert_encoding</code></a> but as you said this will shorten messages to 70 chars - so perhaps you can use this in conjunction with <a href="http://php.net/manual/en/function.mb-detect-encoding.php" rel="nofollow noreferrer"><code>mb_detect_encoding</code></a>?</p>\n\n<p>See: <a href="http://php.net/manual/en/ref.mbstring.php" rel="nofollow noreferrer">Multibyte Functions</a></p>\n'}, {'answer_id': 27623, 'author': 'Magnus Westin', 'author_id': 2957, 'author_profile': 'https://Stackoverflow.com/users/2957', 'pm_score': 2, 'selected': False, 'text': "<p>I know this isnt php code, but I think it might help anyway. This is how I do it in an app I wrote to detect if its possible to send as GSM 03.38 (you could do something similar for plain text). It has two translation tables, one for normal GSM and one for the extended. And then a function that loops through all characters checking if it can be converted.</p>\n\n<pre><code>#define UCS2_TO_GSM_LOOKUP_TABLE_SIZE    0x100\n#define NON_GSM                              0x80 \n#define UCS2_GCL_RANGE                  24\n#define UCS2_GREEK_CAPITAL_LETTER_ALPHA 0x0391\n#define EXTEND                                0x001B\n// note that the ` character is mapped to ' so that all characters that can be typed on\n// a standard north american keyboard can be converted to the GSM default character set\nstatic unsigned char  Ucs2ToGsm[UCS2_TO_GSM_LOOKUP_TABLE_SIZE] =\n{           /*+0x0      +0x1        +0x2        +0x3        +0x4        +0x5        +0x6        +0x7*/\n/*0x00*/    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,\n/*0x08*/    NON_GSM,    NON_GSM,    0x0a,       NON_GSM,    NON_GSM,    0x0d,       NON_GSM,    NON_GSM,\n/*0x10*/    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,\n/*0x18*/    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,\n/*0x20*/    0x20,       0x21,       0x22,       0x23,       0x02,       0x25,       0x26,       0x27,\n/*0x28*/    0x28,       0x29,       0x2a,       0x2b,       0x2c,       0x2d,       0x2e,       0x2f,\n/*0x30*/    0x30,       0x31,       0x32,       0x33,       0x34,       0x35,       0x36,       0x37,\n/*0x38*/    0x38,       0x39,       0x3a,       0x3b,       0x3c,       0x3d,       0x3e,       0x3f,\n/*0x40*/    0x00,       0x41,       0x42,       0x43,       0x44,       0x45,       0x46,       0x47,\n/*0x48*/    0x48,       0x49,       0x4a,       0x4b,       0x4c,       0x4d,       0x4e,       0x4f,\n/*0x50*/    0x50,       0x51,       0x52,       0x53,       0x54,       0x55,       0x56,       0x57,\n/*0x58*/    0x58,       0x59,       0x5a,       EXTEND,     EXTEND,     EXTEND,     EXTEND,     0x11,\n/*0x60*/    0x27,       0x61,       0x62,       0x63,       0x64,       0x65,       0x66,       0x67,\n/*0x68*/    0x68,       0x69,       0x6a,       0x6b,       0x6c,       0x6d,       0x6e,       0x6f,\n/*0x70*/    0x70,       0x71,       0x72,       0x73,       0x74,       0x75,       0x76,       0x77,\n/*0x78*/    0x78,       0x79,       0x7a,       EXTEND,     EXTEND,     EXTEND,     EXTEND,     NON_GSM,\n/*0x80*/    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,\n/*0x88*/    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,\n/*0x90*/    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,\n/*0x98*/    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,\n/*0xa0*/    NON_GSM,    0x40,       NON_GSM,    0x01,       0x24,       0x03,       NON_GSM,    0x5f,\n/*0xa8*/    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,\n/*0xb0*/    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,\n/*0xb8*/    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    0x60,\n/*0xc0*/    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    0x5b,       0x0e,       0x1c,       0x09,\n/*0xc8*/    NON_GSM,    0x1f,       NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    0x60,\n/*0xd0*/    NON_GSM,    0x5d,       NON_GSM,    NON_GSM,    NON_GSM,    NON_GSM,    0x5c,       NON_GSM,\n/*0xd8*/    0x0b,       NON_GSM,    NON_GSM,    NON_GSM,    0x5e,       NON_GSM,    NON_GSM,    0x1e,\n/*0xe0*/    0x7f,       NON_GSM,    NON_GSM,    NON_GSM,    0x7b,       0x0f,       0x1d,       NON_GSM,\n/*0xe8*/    0x04,       0x05,       NON_GSM,    NON_GSM,    0x07,       NON_GSM,    NON_GSM,    NON_GSM,\n/*0xf0*/    NON_GSM,    0x7d,       0x08,       NON_GSM,    NON_GSM,    NON_GSM,    0x7c,       NON_GSM,\n/*0xf8*/    0x0c,       0x06,       NON_GSM,    NON_GSM,    0x7e,       NON_GSM,    NON_GSM,    NON_GSM\n};\n\nstatic unsigned char Ucs2GclToGsm[UCS2_GCL_RANGE + 1] =\n{\n/*0x0391*/  0x41, // Alpha A\n/*0x0392*/  0x42, // Beta B\n/*0x0393*/  0x13, // Gamma\n/*0x0394*/  0x10, // Delta\n/*0x0395*/  0x45, // Epsilon E\n/*0x0396*/  0x5A, // Zeta Z\n/*0x0397*/  0x48, // Eta H\n/*0x0398*/  0x19, // Theta\n/*0x0399*/  0x49, // Iota I\n/*0x039a*/  0x4B, // Kappa K\n/*0x039b*/  0x14, // Lambda\n/*0x039c*/  0x4D, // Mu M\n/*0x039d*/  0x4E, // Nu N\n/*0x039e*/  0x1A, // Xi\n/*0x039f*/  0x4F, // Omicron O\n/*0x03a0*/  0X16, // Pi\n/*0x03a1*/  0x50, // Rho P\n/*0x03a2*/  NON_GSM,\n/*0x03a3*/  0x18, // Sigma\n/*0x03a4*/  0x54, // Tau T\n/*0x03a5*/  0x59, // Upsilon Y\n/*0x03a6*/  0x12, // Phi \n/*0x03a7*/  0x58, // Chi X\n/*0x03a8*/  0x17, // Psi\n/*0x03a9*/  0x15  // Omega\n};\n\nbool Gsm0338Encoding::IsNotGSM( wchar_t szUnicodeChar )\n{\n    bool    result = true;\n    if( szUnicodeChar &lt; UCS2_TO_GSM_LOOKUP_TABLE_SIZE )\n    {\n        result = ( Ucs2ToGsm[szUnicodeChar] == NON_GSM );\n    }\n    else if( (szUnicodeChar &gt;= UCS2_GREEK_CAPITAL_LETTER_ALPHA) &amp;&amp;\n                (szUnicodeChar &lt;= (UCS2_GREEK_CAPITAL_LETTER_ALPHA + UCS2_GCL_RANGE)) )\n    {\n        result = ( Ucs2GclToGsm[szUnicodeChar - UCS2_GREEK_CAPITAL_LETTER_ALPHA] == NON_GSM );\n    }\n    else if( szUnicodeChar == 0x20AC ) // €\n    {\n        result = false;\n    }\n    return result;\n}\n\nbool Gsm0338Encoding::IsGSM( const std::wstring&amp; str )\n{\n    bool    result = true;\n    if( std::find_if( str.begin(), str.end(), IsNotGSM ) != str.end() )\n    {\n        result = false;\n    }\n    return result;\n}\n</code></pre>\n"}, {'answer_id': 68096, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': True, 'text': '<p>To deal with it conceptually before getting into mechanisms, and apologies if any of this is obvious, a string can be defined as a sequence of Unicode characters, Unicode being a database that gives an id number known as a code point to every character you might need to work with. GSM-338 contains a subset of the Unicode characters, so what you\'re doing is extracting a set of codepoints from your string, and checking to see if that set is contained in GSM-338.</p>\n\n<pre><code>// second column of http://unicode.org/Public/MAPPINGS/ETSI/GSM0338.TXT\n$gsm338_codepoints = array(0x0040, 0x0000, ..., 0x00fc, 0x00e0)\n$can_use_gsm338 = true;\nforeach(codepoints($mystring) as $codepoint){\n    if(!in_array($codepoint, $gsm338_codepoints)){\n      $can_use_gsm338 = false;\n      break;\n    }\n}\n</code></pre>\n\n<p>That leaves the definition of the function codepoints($string), which isn\'t built in to PHP. PHP understands a string to be a sequence of bytes rather than a sequence of Unicode characters. The best way of bridging the gap is to get your strings into UTF8 as quickly as you can and keep them in UTF8 as long as you can - you\'ll have to use other encodings when dealing with external systems, but isolate the conversion to the interface to that system and deal only with utf8 internally. </p>\n\n<p>The functions you need to convert between php strings in utf8 and sequences of codepoints can be found at <a href="http://hsivonen.iki.fi/php-utf8/" rel="noreferrer">http://hsivonen.iki.fi/php-utf8/</a> , so that\'s your codepoints() function.</p>\n\n<p>If you\'re taking data from an external source that gives you Unicode slash-escaped characters ("Let\'s test \\u00f6\\u00e4\\u00fc..."), that string escape format should be converted to utf8. I don\'t know offhand of a function to do this, if one can\'t be found, it\'s a matter of string/regex processing + the use of the hsivonen.iki.fi functions, for example when you hit \\u00f6, replace it with the utf8 representation of the codepoint 0xf6.</p>\n'}, {'answer_id': 1441442, 'author': 'jW.', 'author_id': 8880, 'author_profile': 'https://Stackoverflow.com/users/8880', 'pm_score': 3, 'selected': False, 'text': '<p>Although this is an old thread I recently had to solve a very similar problem and wanted to post my answer. The PHP code is somewhat simple. It starts with a painstakingly large array of GSM valid character codes in an array, then simply checks if the current character is in that array using the <a href="http://us2.php.net/ord" rel="noreferrer">ord($string) function</a> which returns the ascii value of the first character of the string passed. Here is the code I use to validate if a string is GSM worth.</p>\n\n<pre><code>    $valid_gsm_keycodes = Array(   \n        0x0040, 0x0394, 0x0020, 0x0030, 0x00a1, 0x0050, 0x00bf, 0x0070,\n        0x00a3, 0x005f, 0x0021, 0x0031, 0x0041, 0x0051, 0x0061, 0x0071,\n        0x0024, 0x03a6, 0x0022, 0x0032, 0x0042, 0x0052, 0x0062, 0x0072,\n        0x00a5, 0x0393, 0x0023, 0x0033, 0x0043, 0x0053, 0x0063, 0x0073,\n        0x00e8, 0x039b, 0x00a4, 0x0034, 0x0035, 0x0044, 0x0054, 0x0064, 0x0074,\n        0x00e9, 0x03a9, 0x0025, 0x0045, 0x0045, 0x0055, 0x0065, 0x0075,\n        0x00f9, 0x03a0, 0x0026, 0x0036, 0x0046, 0x0056, 0x0066, 0x0076,\n        0x00ec, 0x03a8, 0x0027, 0x0037, 0x0047, 0x0057, 0x0067, 0x0077, \n        0x00f2, 0x03a3, 0x0028, 0x0038, 0x0048, 0x0058, 0x0068, 0x0078,\n        0x00c7, 0x0398, 0x0029, 0x0039, 0x0049, 0x0059, 0x0069, 0x0079,\n        0x000a, 0x039e, 0x002a, 0x003a, 0x004a, 0x005a, 0x006a, 0x007a,\n        0x00d8, 0x001b, 0x002b, 0x003b, 0x004b, 0x00c4, 0x006b, 0x00e4,\n        0x00f8, 0x00c6, 0x002c, 0x003c, 0x004c, 0x00d6, 0x006c, 0x00f6,\n        0x000d, 0x00e6, 0x002d, 0x003d, 0x004d, 0x00d1, 0x006d, 0x00f1,\n        0x00c5, 0x00df, 0x002e, 0x003e, 0x004e, 0x00dc, 0x006e, 0x00fc,\n        0x00e5, 0x00c9, 0x002f, 0x003f, 0x004f, 0x00a7, 0x006f, 0x00e0 );\n\n\n        for($i = 0; $i &lt; strlen($string); $i++) {\n            if(!in_array($string[$i], $valid_gsm_keycodes)) return false;\n        }\n\n        return true;\n</code></pre>\n'}, {'answer_id': 12196609, 'author': 'Sergey Shuchkin', 'author_id': 594867, 'author_profile': 'https://Stackoverflow.com/users/594867', 'pm_score': 3, 'selected': False, 'text': '<pre><code>function is_gsm0338( $utf8_string ) {\n    $gsm0338 = array(\n        \'@\',\'Δ\',\' \',\'0\',\'¡\',\'P\',\'¿\',\'p\',\n        \'£\',\'_\',\'!\',\'1\',\'A\',\'Q\',\'a\',\'q\',\n        \'$\',\'Φ\',\'"\',\'2\',\'B\',\'R\',\'b\',\'r\',\n        \'¥\',\'Γ\',\'#\',\'3\',\'C\',\'S\',\'c\',\'s\',\n        \'è\',\'Λ\',\'¤\',\'4\',\'D\',\'T\',\'d\',\'t\',\n        \'é\',\'Ω\',\'%\',\'5\',\'E\',\'U\',\'e\',\'u\',\n        \'ù\',\'Π\',\'&amp;\',\'6\',\'F\',\'V\',\'f\',\'v\',\n        \'ì\',\'Ψ\',\'\\\'\',\'7\',\'G\',\'W\',\'g\',\'w\',\n        \'ò\',\'Σ\',\'(\',\'8\',\'H\',\'X\',\'h\',\'x\',\n        \'Ç\',\'Θ\',\')\',\'9\',\'I\',\'Y\',\'i\',\'y\',\n        "\\n",\'Ξ\',\'*\',\':\',\'J\',\'Z\',\'j\',\'z\',\n        \'Ø\',"\\x1B",\'+\',\';\',\'K\',\'Ä\',\'k\',\'ä\',\n        \'ø\',\'Æ\',\',\',\'&lt;\',\'L\',\'Ö\',\'l\',\'ö\',\n        "\\r",\'æ\',\'-\',\'=\',\'M\',\'Ñ\',\'m\',\'ñ\',\n        \'Å\',\'ß\',\'.\',\'&gt;\',\'N\',\'Ü\',\'n\',\'ü\',\n        \'å\',\'É\',\'/\',\'?\',\'O\',\'§\',\'o\',\'à\'\n     );\n    $len = mb_strlen( $utf8_string, \'UTF-8\');\n\n    for( $i=0; $i &lt; $len; $i++)\n        if (!in_array(mb_substr($utf8_string,$i,1,\'UTF-8\'), $gsm0338))\n            return false;\n\n    return true;\n}\n</code></pre>\n'}, {'answer_id': 13469742, 'author': 'Enyby', 'author_id': 1504248, 'author_profile': 'https://Stackoverflow.com/users/1504248', 'pm_score': 0, 'selected': False, 'text': '<pre><code>preg_match(\'/^[\\x0A\\x0C\\x0D\\x20-\\x5F\\x61-\\x7E\\xA0\\xA1\\xA3-\\xA5\\xA7\'.\n    \'\\xBF\\xC4-\\xC6\\xC9\\xD1\\xD6\\xD8\\xDC\\xDF\\xE0\\xE4-\\xE9\\xEC\\xF1\'.\n    \'\\xF2\\xF6\\xF8\\xF9\\xFC\'.\n    json_decode(\'"\\u0393\\u0394\\u0398\\u039B\\u039E\\u03A0\\u03A3\\u03A6\\u03A8\\u03A9\\u20AC"\').\n    \']*$/u\', $text)\n</code></pre>\n\n<p>or</p>\n\n<pre><code>preg_match(\'/^[\\x0A\\x0C\\x0D\\x20-\\x5F\\x61-\\x7E\\xA0\\xA1\\xA3-\\xA5\\xA7\\xBF\\xC4-\\xC6\\xC9\\xD1\\xD6\\xD8\\xDC\\xDF\\xE0\\xE4-\\xE9\\xEC\\xF1\\xF2\\xF6\\xF8\\xF9\\xFCΓΔΘΛΞΠΣΦΨΩ€]*$/u\', $text)\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/27599', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2951/']