Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching for a string in a file in a different language - PHP - UTF-8

Tags:

php

utf-8

I have read through many posts and tried many thing,

I have some monster files in a game server I am working on, The game is a korean game so a lot of the code words are in korean.

I am trying to get a line that starts with *아이템 followed by the string I am wanting. I set the default_encoding to UTF-8. I am able to find the string based on other bits in it but I want to exclude that *아이템 from my output,

sample for the code is:

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);

however it cannot find the characters, If I pick another part of the line and echo it, the characters show (since I set the default_encoding) but it cannot find it, and its painful as there are many trigger words in the list that I wish to find that are in korean.

Thanks in advance.

example of the file would be :

*아이템 5000 ec101 db120 da120 dg120 

the ec101 etc is what I am trying to pilfer.

have tried mb_stripos unsuccessfully, and tried again with the code supplied below to no avail. it just doesn't find the text, however if I set it to find ec101 it will, but i can't guarantee that will be in the line so I used the preg_match but that only works for the drops, it wont work for all the other bits of information I am trying to find from the files

like image 680
Craig B Avatar asked Oct 17 '22 16:10

Craig B


1 Answers

stripos() is not multibyte compatible. Instead you should use mb_stripos() which should work better for you. Also note that you need to check explicitly for a false result. A result of zero can also be interpreted as false.

$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";
    }
}
like image 53
miken32 Avatar answered Oct 20 '22 15:10

miken32