字符串和文件名之间的PHP字符编码
我有一个写得很差的html文件(从Windows程序导出,所以我无法控制它),我想解析它。我使用的是正则表达式,我知道它对HTML不是很好,但我知道它的结构不会改变,所以它工作得很好 数据如下所示: 起初,文件内容不起作用,但Firefox告诉我它被编码为UTF-16。所以我把它编码成这样:字符串和文件名之间的PHP字符编码,php,encoding,utf-8,character-encoding,Php,Encoding,Utf 8,Character Encoding,我有一个写得很差的html文件(从Windows程序导出,所以我无法控制它),我想解析它。我使用的是正则表达式,我知道它对HTML不是很好,但我知道它的结构不会改变,所以它工作得很好 数据如下所示: 起初,文件内容不起作用,但Firefox告诉我它被编码为UTF-16。所以我把它编码成这样: $shortlist_html = mb_convert_encoding(file_get_contents($this->html_loc), 'UTF-8', 'UTF-16'); 然后,我
$shortlist_html = mb_convert_encoding(file_get_contents($this->html_loc), 'UTF-8', 'UTF-16');
然后,我从td之间获取我想要的数据,但有时数据具有类似这样的奇怪字符(请注意奇怪的I):
阿德米尔森
但是,我还有一个目录,里面有我想要循环浏览的图像,它们的名称如下:
Ademílson_uAttributes.png
Ademílson_uustats.png
Ademílson_uprofile.png
现在我想将名称与图像名称进行匹配。我可以很容易地用正则表达式得到名字,但是直接匹配是不行的。如果我var_转储我得到的字符串:
阿德姆�莱森
如果我对它进行utf-8编码,然后var_转储它,它就去掉了“í”
阿德尔森
以上两项都与我的字符串不匹配
我应该注意,当我说match时,我实际上是在做一个isset,例如:
$myarray = array('Ademílson'=>57);
$handle = opendir($image_dir);
while($filename = readdir($handle)) //$filename is Ademílson_ Attributes.png
{
preg_match('/(.*?)_+/', $filename, $matches);
$playername = $matches[1]; //$playername is Ademílson but appears as Adem�lson when using var_dump
if(isset($myarray[$playername]))
{
//This never matches
}
foreach($myarray as $name => $id)
{
if($name == $playername)
{
//This never matches
}
}
//Okay try and utf-8 it instead
$playername = mb_convert_encoding($playername, 'utf-8', mb_detect_encoding($playername)); //This time a var_dump strips the "i" e.g. Ademlson
if(isset($myarray[$playername]))
{
//This still never matches
}
foreach($myarray as $name => $id)
{
if($name == $playername)
{
//This still never matches
}
}
}
我希望我已经充分解释了这一点。基本上,当字符串包含异常字符时,我希望将从文件名检索的字符串与从文件内容检索的字符串进行匹配。1)在var\u转储字符串之前是否发送编码头?如果不是,这可能是产生误导性«的原因�»而不是«í»。因此,在isset()或foreach()例程中,仍然应该检查«í»。2) 您需要引号来检查数组键。isset($myarray['Ademílson']);抱歉,那只是伪代码。显然,isset使用的是一个变量。也许我应该更新它。好的@andr我编辑了问题中的代码发送标题和不使用引号不是问题,因为这只是伪代码:)我想你可能需要使用PHP mb_u函数集,例如mb_ereg_replace。你能更具体一点吗?我看不出一个mb_ereg_替换会对我有什么帮助,而且mb_ereg_match看起来不像preg_match那样有效,所以这不是你想要的。