php - 在不同语言的文件中搜索字符串 - PHP - UTF-8

标签 php utf-8

我已经阅读了很多帖子并尝试了很多东西,

我正在处理的游戏服务器中有一些怪物文件, 该游戏是韩语游戏,所以很多暗语都是韩语。

我正在尝试获取以 *아이템 开头的行,后跟我想要的字符串。我将 default_encoding 设置为 UTF-8。 我能够根据其中的其他位找到该字符串,但我想从我的输出中排除 *ans이템

代码示例是:

ini_set("max_execution_time", 0);
    $monsdbconn = sqlsrv_connect("INSTANCE\SQLEXPRESS", array("Database" => "MonsDB", "UID" => "BLAH", "PWD"=> "BLAH"));
    $monsDir = realpath('C:/PT-Server/GameServer/Monster/');
    $monsters = new RecursiveDirectoryIterator($monsDir);

if (@$monsdbconn) {
    $clearit = "DELETE FROM monsdrops";
    if (sqlsrv_query($monsdbconn,$clearit)) {
        foreach($monsters as $name => $object){
            $monstername = "";  
            if (stripos($name, '.inf')){
                $monsterfile = file($name);
                $items = array("WA*", "WP*", "DA*", "WC*");
                foreach ($monsterfile as $monster) {
                    if (strstr($monster, "Name")) {
                        //things to remove from the string.
                        $monstrip = array("*Name",'"'); 

                        //Remove "" and *Name from the string
                        $monstername = str_replace($monstrip, "", $monster); 

                        //Remove spaces from start and end of string to prevent
                        //Duplicate entries, Will not remove space from between words.
                        $monstername = trim($monstername," "); // Space
                        $monstername = trim($monstername,"  "); // Tab
                    }
                    // THIS IS THE POINT IM SEARCHING FOR ITEMS AT THE MOMENT, BUT I NEED IT TO FIND THE KOREAN CHAR SET
                    if (preg_match("/\D{2}\d{3}/", $monster)) { 

                        $string = preg_split("/(\s)/", $monster);
                        foreach ($string as $line) {
                            if ((preg_match("/\D{2}\d{3}/", $line)) && ((stripos($line, "name\\") === false) || stripos($line, ".zhoon") === false)) {
                                $sqlinsert = "INSERT INTO monsdrops ([monstername],[monsterdrops]) VALUES ('$monstername', '$line')";
                                $insert = sqlsrv_query($monsdbconn, $sqlinsert);
                                if ($insert) {
                                    echo "Insert $monstername, $line Successful! <br />";       
                                } else {
                                    echo "<br />Insert Failed! <br />";
                                    print_r(sqlsrv_errors());
                                }
                            }
                        }
                    }       
                }

            }
        }
    } else {
        echo "Unable To Clear DB";
    }
} else {
    echo "Unable to connect to DB";
}
@sqlsrv_close($monsdbconn);

但是它找不到字符, 如果我选择该行的另一部分并回显它,字符会显示(因为我设置了 default_encoding)但它找不到它,这很痛苦,因为列表中有很多我希望找到的触发词是韩语。

提前致谢。

文件的例子是:

*아이템 5000 ec101 db120 da120 dg120 

ec101 等是我想偷的东西。

已尝试 mb_stripos 失败,并使用下面提供的代码再次尝试无济于事。它只是找不到文本, 但是,如果我将它设置为查找 ec101,它会找到,但我不能保证它会在行中,所以我使用了 preg_match,但它只适用于掉落,它不适用于我尝试的所有其他信息从文件中查找

最佳答案

stripos() 不兼容多字节。相反,您应该使用 mb_stripos()哪个更适合你。另请注意,您需要明确检查错误结果。结果为零也可以解释为假。

$file = "c:\server\monster.inf";
$lines = file($file);
foreach ($lines as $line) {
    // convert to Unicode standard
    $line = mb_convert_encoding($line, "UTF-8", "EUC-KR");
    if (mb_stripos($line, "*아이템") !== false) {
        echo "$line\n";
    }
}

关于php - 在不同语言的文件中搜索字符串 - PHP - UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42336632/

相关文章:

java - Netbeans - .properties 文件默认编码 |奇怪的行为

java - 在 Java 中,确保获得 UTF-8 字符串的最佳方法是什么?

php - mysql 开放时间超过午夜

php - 我可以在这里使用 foreach 进行多次上传吗?

java - 删除 String 中的 0 字节 (UTF-8) 字符

javascript - 最高的 Unicode 字符?

php - JSON_NUMERIC_CHECK 和电话号码

php file_uploads 在服务器上被禁用,解决方法?

php - 抓取任何以 http 开头的字符串,直到 php 中的第一个空格字符

ruby-on-rails - Wicked_pdf 和 utf8 符号