dqzuo0327 2016-01-20 11:00
浏览 33
已采纳

PHP - 来自csv的字符串报告更多字符

So, I'm making this easy register check page, where a user can enter an ID, and get the results matching that ID from a CSV file.

Problem I'm having, is that for some reason I can't get a good match. Or, it matches every single line.

var_dump on the two comparing strings gives me this:

string(7) "AKM" string(3) "AKM"

AKM is the userID in this example. I've tried utf8 encoding and tried searching eeverywhere. Just cant figure out why I do not get a match, and why var_dump reports AKM to be 7 characters.

Any idea?

Code below:

$file = 'report/report.csv';
$startRow = 6;
$hms = $_GET["hms"];

$csv = array_map(function($v){return str_getcsv($v, "\t");}, file($file));
$csv = array_slice($csv, 6);

foreach($csv as $row){
if (strpos($row,$hms) !== false) {
    $start = strpos($row[1], '(') - 2;
    $end = strlen($row[1]) - strpos($row[1], ')');

    $userID = substr($row[1],0,$start);
    $userName = substr($row[1],$start+3,-2);

    if($row[3] == "true"){
        $passed = "Bestått (".$row[4].")";
    }else{
        $passed = "Ikke bestått";
    }

    $result = [
        "id" => $userID,
        "name" => $userName,
        "passed" => $passed, 
        "datePassed" => $row[7],
        "timeSpent" => $row[6],
    ];

    var_dump($result);
}
}

This is how the CSV looks like:

response

data

identity_uid identity_caption completion passed result attempts time last_access U_5A69E39671310F44A2333C9206DAD20C xxx (Name) 100.0 true 82.6 3 00:57:09 2016/01/04 10:28:54 U_DF5A47B8E1A6E247AA338F571A8F63B1 yyy (Name) 5.0 false 0.0 3 00:04:48 2016/01/15 07:26:05

EDIT: This is the hidden characters I found now:

'' . "\0" . 'A' . "\0" . 'K' . "\0" . 'M' . "\0" . ''
  • 写回答

1条回答 默认 最新

  • dongmubei7950 2016-01-20 11:26
    关注

    The UTF-8 encoding for Unicode Character 'LATIN CAPITAL LETTER A' (U+0041) is 0x41. Since you have 0x0041 your file is UTF-16 Big Endian.

    If you app uses UTF-8 you'll have to convert. You can use iconv() or mb_convert_encoding().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 qiankun主应用注册子应用提示跨域
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?
  • ¥15 快手小店商家版APP怎么第三方APP跳转到指定用户聊天界面
  • ¥15 为什么AVL fire软件仿真时的步长改变了,仿真结果也会改变
  • ¥100 如何帮我写一个java小游戏