简体   繁体   English

PHP字符串比较不起作用,正在读取文件

[英]PHP string comparison not working, file reading

I am using PHP to read in a tab delimited CSV file and a pipe delimited TXT file. 我正在使用PHP读取制表符分隔的CSV文件和管道分隔的TXT文件。 Unfortunately, I cannot get a string comparsion to work even though the characters (appear) to be exactly the same. 不幸的是,即使字符(看起来)完全相同,我也无法进行字符串比较。 I used trim to make sure to clean up hidden characters and I even tried type-casting to string. 我使用trim来确保清除隐藏的字符,甚至尝试将类型转换为字符串。

Var dump shows they are clearly different but I am not sure how to make them the same? Var dump显示它们明显不同,但是我不确定如何使它们相同?

// read in CSV file
$fh = fopen($mapping_date, 'r');
$mapping_data = fread($fh, filesize($mapping_date));
... 
// use str_getcsv to put each line into an array
// get values out that I want to compare

$this_strategy = (string)trim($strategy_name);
$row_strategy = (string)trim($row3[_Strategy_Name]);

if($this_strategy == $row_strategy) { // do something }

var_dump($this_strategy);

Vardump: string(16) "Low Spend ($0.2)" Vardump:字符串(16)“低支出($ 0.2)”

var_dump($row_strategy);

Vardump: string(31) "Low Spend ($0.2)" Vardump:字符串(31)“支出偏低($ 0.2)”

Can't figure out for the life of me how to make this work. 我一生都想不通如何进行这项工作。

Looks like you have the database encoded in UCS2 (assuming it's MySQL). 看起来您已经用UCS2编码了数据库(假设它是MySQL)。 http://dev.mysql.com/doc/refman/5.1/en/charset-unicode-ucs2.html http://dev.mysql.com/doc/refman/5.1/en/charset-unicode-ucs2.html

You can use possibly use iconv to convert the format - but there's an example in the comments on that page (but it doesn't use iconv - http://php.net/manual/en/function.iconv.php#49171 ). 您可以使用iconv来转换格式-但该页面上的注释中有一个示例(但它不使用iconv- http://php.net/manual/zh/function.iconv.php#49171 ) 。 I've not tested it. 我还没有测试。

Alternatively, change the database field encoding to utf8_generic or ASCII or whatever the file is encoded as? 或者,将数据库字段编码更改为utf8_generic或ASCII或任何文件编码为?


Edit: Found the actual PHP function you want: mb_convert_encoding - UCS2 is one of the supported encodings, so enable that in php ini and you're good to go. 编辑:找到所需的实际PHP函数: mb_convert_encoding -UCS2是受支持的编码之一,因此在php ini中启用该功能,您就可以开始了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM