Working with files and utf8 in PHP -


Let's say I have a file that is encoded in utf8 called foo.txt:

< And I want to get that array containing all the rows in that file (one row per index) in which the letters are AUPIF, and These lines are accompanied by lines only.

I have written the following code (also encoded as Youtube):

  $ permissions_lets = array ("a", "o", "e "," U "," ñ "," p "," y "," f "); $ Lines = array (); $ F = fopen ("foo.txt", "r"); While (! FIF ($ f)) {$ line = fgets ($ f); Foreach (preg_split ("//", $ line, -1, PREG_SPLIT_NO_EMPTY) as the $ character) {if (! In_array ($ letters, $ permission_letters)) {$ line = ""; }} If ($ line! = "") {$ Lines [] = $ line; }} Fclose ($ f);  

However, after that, there is only Au line in the $ lines array.
It seems because in some way, "code" $ allow_locateres in "ñ" In Txt the "ñ" is not the same.
If I print "ñ" of the file, a question mark appears, but if I print it like this print "ñ"; , it works.
How can I work it out?

If you are running Windows, the OS does not save files in UTF-8, but CP By default, 1251 (or something ...) you need to explicitly save the file in that format or line utf8_encode () before checking your Ie. > $ line = utf8_encode (fgets ($ f));

If you are sure the file is UTF-8 encoded, is your PHP file also UTF-8 encoded?

If everything is UTF-8, then you need it:

  foreach (preg_split ("// u", $ line, -1, PREG_SPLIT_NO_EMPTY) $ Letter as) {// ...}   

( u for unicode characters)

< P> However, let me suggest a quick way to check your account:

$ permissions_lays = array ("one", "o", "e", "u", "n" "P", "y", "f"); $ Lines = array (); $ F = fopen ("foo.txt", "r"); While (! FIF ($ f)) {$ line = fgets ($ f); $ Line = str_split (rtrim ($ line)); If (count (array_directory ($ line, $ permissions_letters)) == count ($ line)) {$ lines [=] $ line; }} Fclose ($ f);

(To allow space characters also add space characters and rtrim ($ line) )


Comments