如何在php中用指定的分隔符拆分unicode字符串?
对不起,如果这个问题听起来没问题。我的问题是我有一个文本文件,需要将数据导入数据库 读取过程很好,当我打印行时,它是正确的 然后我需要按制表符(\t)分隔符将其拆分。然后所有的unicode字符都被打断 我是这样尝试的:如何在php中用指定的分隔符拆分unicode字符串?,php,unicode,split,Php,Unicode,Split,对不起,如果这个问题听起来没问题。我的问题是我有一个文本文件,需要将数据导入数据库 读取过程很好,当我打印行时,它是正确的 然后我需要按制表符(\t)分隔符将其拆分。然后所有的unicode字符都被打断 我是这样尝试的: <!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>Test page for project XY</title>
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>Test page for project XY</title>
</head>
<body>
<h1>Test Page</h1>
<pre>
<?php
include 'ChromePhp.php';
ChromePhp::log('Start read file!');
ini_set("default_charset", 'utf-8');
$handle = fopen("input.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
// process the line read.
print_r($line); // if print the $line here, it's fine
$myArray = preg_split("/[\t]/", $line); //=> broken unicode, adding u not work
$myArray = explode("\t", $line);//=> same
print_r($myArray);
}
fclose($handle);
} else {
// error opening the file.
// ChromePhp::log('Cant open file!');
print_r ('Cant open file!');
}
?>
</pre>
</body>
</html>
试着在终端上运行它,我想你们看到了错误。试着把头指令放在php中,并说它是codepage=UTF-8。您还需要检查php文件代码页本身。您可能只需要unicode修饰符:
preg\u split(“/[\t]/u“,$line)
Hi,我尝试了preg\u split(“/[\t]/u“,$line)
但是像这样的字符�� 仍然出现:(您的php源文件也是utf-8吗?是的,是的。实际上,它是utf-8,没有BOM表。我尝试切换到utf-8,但没有任何更改:(感谢您的关注。事实证明,input.txt是用UCS-2:p编码的,这是我的错。)
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
$tab="\t";
$myArray=(mb_split($tab,$line));
print_r($myArray);`