I have a PHP program that reads a certain FILE from an INVENTORY SCANNER. The data is streamed in 1 line like this.
3701804901070125616シャルダン ステキプラスクルマ専用 ジャスミンマリアシャルダン ステキプラスクルマ ジ2131970080 16033001383701804902720123549森永IQサポート もも&りんご 125ml×3 モリナガIQサポート モモ&リンゴ1901030080 16033001383701804987072042557噛むブレスケア マスカット 25粒 ブレスケアカム 25T マスカット 2121070080 1603300138
These can be separated by every 128 bytes. Like this
Here is the hex of the string with the width of 256 bytes
0 : 33 37 30 31 38 30 34 39 30 31 30 37 30 31 32 35 36 31 36 e3 82 b7 e3 83 a3 e3 83 ab e3 83 80 e3 83 b3 e3 80 80 e3 82 b9 e3 83 86 e3 82 ad e3 83 97 e3 83 a9 e3 82 b9 e3 82 af e3 83 ab e3 83 9e e5 b0 82 e7 94 a8 e3 80 80 e3 82 b8 e3 83 a3 e3 82 b9 e3 83 9f e3 83 b3 e3 83 9e e3 83 aa e3 82 a2 ef bd bc ef bd ac ef be 99 ef be 80 ef be 9e ef be 9d 20 ef bd bd ef be 83 ef bd b7 ef be 8c ef be 9f ef be 97 ef bd bd ef bd b8 ef be 99 ef be 8f 20 ef bd bc ef be 9e 32 31 33 31 39 37 30 30 38 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 30 33 33 30 30 31 33 38 33 37 30 31 38 30 34 39 30 32 37 32 30 31 32 33 35 34 39 e6 a3 ae e6 b0 b8 ef bc a9 ef bc b1 e3 82 b5 e3 83 9d e3 83 bc e3 83 88 e3 80 80 e3 82 82 e3 82 82 ef bc 86 e3 82 8a e3 82 93 e3 81 94 e3 80 [3701804901070125616................................................................................................ .............................. ......2131970080 16033001383701804902720123549...............................................] 100 : 80 ef bc 91 ef bc 92 ef bc 95 ef bd 8d ef bd 8c c3 97 ef bc 93 e3 80 80 20 20 20 20 ef be 93 ef be 98 ef be 85 ef bd b6 ef be 9e 49 51 ef bd bb ef be 8e ef be 9f ef bd b0 ef be 84 20 ef be 93 ef be 93 26 ef be 98 ef be 9d ef bd ba ef be 9e 31 39 30 31 30 33 30 30 38 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 30 33 33 30 30 31 33 38 33 37 30 31 38 30 34 39 38 37 30 37 32 30 34 32 35 35 37 e5 99 9b e3 82 80 e3 83 96 e3 83 ac e3 82 b9 e3 82 b1 e3 82 a2 e3 80 80 e3 83 9e e3 82 b9 e3 82 ab e3 83 83 e3 83 88 e3 80 80 ef bc 92 ef bc 95 e7 b2 92 e3 80 80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ef be 8c ef be 9e ef be 9a ef bd bd ef bd b9 ef bd b1 ef bd b6 ef be 91 20 32 35 54 20 ef be 8f ef bd bd ef bd b6 ef bd af ef be 84 20 20 32 31 32 31 [........................ ...............IQ............... ......&............1901030080 16033001383701804987072042557...................................................... ........................ 25T ............... 2121] 200 : 30 37 30 30 38 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 30 33 33 30 30 31 33 38 [070080 1603300138]
SO here is my PHP that reads the file.
$fp = fopen($vanReadfile, "r");
flock($fp, LOCK_SH);
flock($fp, LOCK_UN);
$vandataBuf= fgets($fp); //fgets since only 1 continously line
$convertBufstring = mb_convert_encoding($vandataBuf, "UTF-8","Shift-JIS");
$tenpo_cd =mb_substr($convertBufstring,$i+0,3,"UTF-8");
$chiku_cd =mb_substr($convertBufstring,$i+3,2,"UTF-8");
$shori_kbn =mb_substr($convertBufstring,$i+5,1,"UTF-8");
$jan_cd =mb_substr($convertBufstring,$i+6,13,"UTF-8");
$prod_nm =mb_substr($convertBufstring,$i+19,52,"UTF-8");
$prod_kn =mb_substr($convertBufstring,$i+71,20,"UTF-8");
$jicfs_class_cd =mb_substr($convertBufstring,$i+91,6,"UTF-8");
$prod_tax =mb_substr($convertBufstring,$i+97,3,"UTF-8");
$regi_duty_kbn =mb_substr($convertBufstring,$i+100,1,"UTF-8");
$auto_order_kbn =mb_substr($convertBufstring,$i+101,1,"UTF-8");
$spacex16 =mb_substr($convertBufstring,$i+102,16,"UTF-8");
The output of the file looks like this using the code above.
TENPO: 370
JAN CD: 4901070125616
PROD NAME: シャルダン ステキプラスクルマ専用 ジャスミンマリアシャルダン ステキプラスクルマ ジ213197
PROD NAME KN: 0080
JICFS CLASS CD: 16033
REGI DUTY KBN: 3
AUTO ORDER KBN: 8
SPACE 3701804902720123
The output I want should look something like this:
TENPO: 370
JAN CD: 4901070125616
**PROD NAME**: シャルダン ステキプラスクルマ専用 ジャスミンマリア
**PROD NAME KN**: シャルダン ステキプラスクルマ ジ
JICFS CLASS CD: 213197
REGI DUTY KBN: 3
AUTO ORDER KBN: 8
SPACE (16 white spaces here)
All other substr() are correct. The problem is when reading the PRODUCT NAME(FULL-width). The program reads sometimes less or more.
here is an example:
シャルダン ステキプラスクルマ専用 ジャスミンマリア
<--- this is 26 characters(72bytes)
森永IQサポート もも&りんご 125ml×3
<--- this is 28 characters(200bytes)
With this, I am already out of ideas on how to deal with this. Can anyone suggest a solution to this?
I tried using
mb_convert_encoding($vandataBuf, "UTF-8","Shift-JIS");
and it didn't do the job.
I also tried adjusting how many characters this code reads and changed it to 26 characters. It works in the first line since its 26 characters but it reads the 2nd line wrong which has 28 characters
$prod_nm =mb_substr($convertBufstring,$i+19,26,"UTF-8");
I also tried converting all full-width characters to half-width but the number of characters also changes so its not consistent.
$convertBufstring = mb_convert_kana($convertBufstringBEFORE, "KansC");
$prod_nm =mb_strcut($convertBufstring,$i+19,52,"UTF-8");
I am already out of ideas. Can anyone suggest something? Maybe I missed something.