CSV 中的字符串添加隐藏字符,因此比较时结果始终为 false

Jay*_*vey 1 php csv string comparison

我正在从 CSV 中提取数据并与其进行比较

private function isCsvValid($fileLocation){
    $file = fopen($fileLocation, 'r');
    $line1 = fgetcsv($file);
    $isValid = $this->checkCorrectColumns($line1);
}


private function checkCorrectColumns($line){
    $columnsFound = array();

    foreach ($line as $value) {
       //comparisons here
       if($value == "GBPAUD"){

       }
    }

}
Run Code Online (Sandbox Code Playgroud)

但是 CSV 字符串中隐藏了字符

    array(6) {
      [0]=> string(19) "363533840"
      [1]=> string(15) "GBPAUD"
      [2]=> string(47) "2007-12-02 17:00:33.000"
      [3]=> string(17) "2.326500"
      [4]=> string(17) "2.327400"
      [5]=> " string(5) "D
    }
Run Code Online (Sandbox Code Playgroud)

我的字符串必须纠正字符数量。

string(6) "GBPAUD"
Run Code Online (Sandbox Code Playgroud)

我可以使用这段正则表达式使比较返回 true,该正则表达式删除了除字母和数字之外的所有内容,这很棒。

/[^A-Za-z0-9]/
Run Code Online (Sandbox Code Playgroud)

但这会删除浮动中的 . 和日期等中的 -

每一位数据都有额外的字符。我不想为每个字符串都有一个定制的正则表达式。

谁能告诉我如何使字符串成为正确的位数?我在互联网上搜索,但没有一个建议有效(例如二进制安全比较、剥离 html 实体等......)

无论如何,我可以删除所有字符串上的所有隐藏字符吗?

Jay*_*vey 6

我找到了解决方案,使用这个正则表达式

$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $value);
Run Code Online (Sandbox Code Playgroud)