Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1097467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:26:15+00:00 2026-05-17T00:26:15+00:00

Lets say I have a file called foo.txt encoded in utf8: aoeu qjkx ñpyf

  • 0

Lets say I have a file called foo.txt encoded in utf8:

aoeu  
qjkx
ñpyf

And I want to get an array that contains all the lines in that file (one line per index) that have the letters aoeuñpyf, and only the lines with these letters.

I wrote the following code (also encoded as utf8):

$allowed_letters=array("a","o","e","u","ñ","p","y","f");

$lines=array();
$f=fopen("foo.txt","r");
while(!feof($f)){
    $line=fgets($f);
    foreach(preg_split("//",$line,-1,PREG_SPLIT_NO_EMPTY) as $letter){
        if(!in_array($letter,$allowed_letters)){
            $line="";
        }
    }
    if($line!=""){
        $lines[]=$line;
    }
}
fclose($f);

However, after that, the $lines array just has the aoeu line in it.
This seems to be because somehow, the “ñ” in $allowed_letters is not the same as the “ñ” in foo.txt.
Also if I print a “ñ” of the file, a question mark appears, but if I print it like this print "ñ";, it works.
How can I make it work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:26:16+00:00Added an answer on May 17, 2026 at 12:26 am

    If you are running Windows, the OS does not save files in UTF-8, but in cp1251 (or something…) by default you need to save the file in that format explicitly or run each line in utf8_encode() before performing your check. I.e.:

    $line=utf8_encode(fgets($f));
    

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

    If everything is UTF-8, then this is what you need :

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

    (append u for unicode chars)

    However, let me suggest a yet faster way to perform your check :

    $allowed_letters=array("a","o","e","u","ñ","p","y","f");
    
    $lines=array();
    $f=fopen("foo.txt","r");
    while(!feof($f)){
        $line=fgets($f);
    
        $line = str_split(rtrim($line));
        if (count(array_intersect($line, $allowed_letters)) == count($line)) {
                $lines[] = $line;
        }
    }
    fclose($f);
    

    (add space chars to allow space characters as well, and remove the rtrim($line))

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file lets say c:\file1.txt that has an ADS file lets say
Ok lets say I have a File called File.txt and it contains Hello World
lets say i have 5 lines within a txt file called users.txt each line
Lets say we have a log file (preferably a txt file) available in the
Lets say I have 10 lines in a file. I have 2 parameters that
I have a header file, lets say Common.h, that is included in all of
Lets say I want to create a document out of user-inputted code, called spam.txt
I have a file lets say it is called filename.c I am trying to
So lets say i have a php file that contains $title=Website | Categories; And
Lets say I have the following file in called index in the directory D:\Experimental:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.