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 6207169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:34:56+00:00 2026-05-24T05:34:56+00:00

I have been trying to extract specific words from an array which was created

  • 0

I have been trying to extract specific words from an array which was created from a text file using file() function in php.

The text file sample.txt is like this:

A registration has been received. 

Name: test
Lastname: test2 
Email: test@test.com
Company/School: Test2
Company2: test3
Address1: test4
Address2: test5
City: test6
State: test7
Country: test8
Zipcode: test9

Now I have used file() function to put this text file into an array.

$file_name='sample.txt';
$file_array=file($file_name);

Then I traversed through loop to extract each value and search for word say ‘Name’ from this array.

    $data1='Name';

    foreach($file_array as $value){
    if(stripos($value,$data1)===FALSE)
        echo "Not Found";
     else 
       echo "Found";
    }

But it always prints ‘Not Found’. I have tried using strpos,strstr, preg_match but to no avail. Also if I use a normal array of words instead of creating from file, it works properly.

Thanks in Advance.

Update: My objective in this problem is to first detect what field it is ex. ‘Name’ and then its value ex. ‘test’

  • 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-24T05:34:57+00:00Added an answer on May 24, 2026 at 5:34 am

    It certainly could be a line ending problem or an issue with your file encoding, I’m not sure exactly how file() handles whitespace either.

    As a suggestion on how to improve the code, if you create yourself a keyed array from the data then you can use the more reliable array_key_exists() function to search for your fields.

    $file_name = 'sample.txt';
    $file_data = file($file_name);
    
    // loop through each line
    foreach ($file_data as $line) {
    
    // from each line, create a two item array
    $line_items = explode(":", $line);
    
    // build an associative (keyed) array
    // stripping whitespace and using lowercase for keys
    $file_array[trim(strtolower($line_items[0]))] = trim($line_items[1]);
    }
    

    Now you can use array_key_exists as follows:

    if (array_key_exists("name", $file_array) === false) {
      print "Not found.";
    } else {
      print "Found.";  
      // and now it's simple to get that value
      print "<br />Value of name:  " . $file_array['name'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using XPath with scrapy to extract text from html tags online,
I have been able to extract certain lines from a large tab-separated text file
I have been trying to extract hours, seconds and minutes from an input text
I am trying the create a loop to extract data from an array created
I have been trying to read an xml file. I have to extract value
I am trying to extract values from an XML file using linq to create
I have been attacking atoi from several different angles trying to extract ints from
I have been going round in circles trying to extract meta tag information from
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I have been trying to make a case for using Python at my work.

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.