Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP utf8 problem
    text
    copied!<p>I have some problems comparing an array with Norwegian characters with a utf8 character.</p> <p>All characters except the special Norwegian characters(æ, ø, å) works fine.</p> <pre><code>function isNorwegianChar($Char) { $aNorwegianChars = array('a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z', 'æ', 'Æ', 'ø', 'Ø', 'å', 'Å', '=', '(', ')', ' ', '-'); $iArrayLength = count($aNorwegianChars); for($iCount = 0; $iCount &lt; $iArrayLength; $iCount++) { if($aNorwegianChars[$iCount] == $Char) { return true; } } return false; } </code></pre> <p>If anyone has any idea about what I can do pleas let me know.</p> <p><strong>Update:</strong></p> <p>The reason for needing this is that I'm trying to parse a text file that contains lines with Norwegian and Chinese words, like a dictionary. I want to split the line in to strings, one containing the Norwegian word and one containing the Chinese. This will later be inserted in a database. Example lines:</p> <p>impulsiv 形 衝動的</p> <p>imøtegå 動 反對,反駁</p> <p>imøtekomme 動 符合</p> <p>alkoholmisbruk(er) 名 濫用酒精 (名 濫用酒精的人)</p> <p>alkoholpåvirket 形 受酒精影響的</p> <p>alkotest 名 呼吸性酒精測試</p> <p>alkymi(st) 名 煉金術 (名 煉金術士)</p> <p>all, alt, alle, 形 全部, 所有 </p> <p>As you can see there might be spaces between the words so I can not use something easy like explode to split between the Chinese and Norwegian words. What I do is use the isNorwegianChar and loop through the line until I find a char that is not in the array.</p> <p>The problem is that it æ, ø and å is not returned as a Norwegian character and it think the Chinese word has started.</p> <p>Here is the code:</p> <pre><code> //Open file. $rFile = fopen("norsk-kinesisk.txt", "r"); // Loop through the file. $Count = 0; while(!feof($rFile)) { if(40== $Count) { break; } $sLine = fgets($rFile); if(0 == $Count) { $sLine = mb_substr($sLine, 3); } $iLineLength = strlen($sLine); $bChineseHasStarted = false; $sNorwegianWord = ''; $sChineseWord = ''; for($iCount2 = 0; $iCount2 &lt; $iLineLength; $iCount2++) { $char = mb_substr($sLine, $iCount2, 1); if(($bChineseHasStarted === false) &amp;&amp; (false == isNorwegianChar($char))) { $bChineseHasStarted = true; } if(false === $bChineseHasStarted) { $sNorwegianWord .= $char; } else { $sChineseWord .= $char; } //echo $char; } $sNorwegianWord = trim($sNorwegianWord); $sChineseWord = trim($sChineseWord); $Count++; } fclose($rFile); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload