473,657 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Beginner] Reading a file into an array (hash)

Hello!

I'm a beginner at PHP and I wonder how I would go about to read a textfile
into an array(hash).

The textfile looks like this:

A/S=age/sex?
A/S/L=age/sex/location?
AA=alcoholics anonymous
AAA=travel club
AAAAA=travel club for alcoholics / american association against acronym
abuse
AAMOF=as a matter of fact
....

It's a file of acronyms, where the first part (before the =) is the acronym
and the second part is the explanation.

I want to read this into an array so that I can access the array with the
acronym and get the explanation. e.g.
$key = 'AAA';
$expl = $acro[$key]

How do I accomplish this in php?

// Anders
Jul 17 '05 #1
2 2150

Anders Eriksson wrote:
Hello!

I'm a beginner at PHP and I wonder how I would go about to read a textfile into an array(hash).

The textfile looks like this:

A/S=age/sex?
A/S/L=age/sex/location?
AA=alcoholics anonymous
AAA=travel club
AAAAA=travel club for alcoholics / american association against acronym abuse
AAMOF=as a matter of fact
...

It's a file of acronyms, where the first part (before the =) is the acronym and the second part is the explanation.

I want to read this into an array so that I can access the array with the acronym and get the explanation. e.g.
$key = 'AAA';
$expl = $acro[$key]


Take a look at the function parse_ini_file( ) at
http://www.php.net/parse_ini_file

Given the PHP file:
<?
$ac = parse_ini_file( 'testfile.ini') ;
print_r ($ac);
?>

and the file 'testfile.ini' containing:
A/S=age/sex?
A/S/L=age/sex/location?
AA=alcoholics anonymous
AAA=travel club
AAAAA=travel club for alcoholics / american association against acronym
abuse
AAMOF=as a matter of fact

The results are:
Array
(
[A/S] => age/sex?
[A/S/L] => age/sex/location?
[AA] => alcoholics anonymous
[AAA] => travel club
[AAAAA] => travel club for alcoholics / american association
against acronym abuse
[AAMOF] => as a matter of fact
)
Ken

Jul 17 '05 #2
Hello Ken,

On 3 May 2005 06:49:00 -0700, Ken Robinson wrote:
Anders Eriksson wrote:
I'm a beginner at PHP and I wonder how I would go about to read a

textfile
into an array(hash).

Take a look at the function parse_ini_file( ) at
http://www.php.net/parse_ini_file

Given the PHP file:
<?
$ac = parse_ini_file( 'testfile.ini') ;
print_r ($ac);
?>


Unfortunately can't the parse_ini_file read all characters. As soon as it
finds a ( ) ! " it stops.

So I found an user function that does the same thing and that can read all
characters.

Thank you for your help!

// Anders
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
5060
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
6
4460
by: Chang | last post by:
How to get SHA1 or MD5 of a big file (+5MB - 20GB) as I can't read 20GB into memory. -- Chang.
5
2522
by: Victor Paraschiv | last post by:
I need to serialize into an XML file a hashtable. From MSDN at XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlserializationxmlserializerclasstopic.asp I implemented the indexable property and the add function but I'm still getting the following exception:"The type System.Collections.Hashtable is not supported because it implements IDictionary." My code for Item and Add add-on...
9
2017
by: me | last post by:
Hi All, I am new to Classes and learniing the ropes with VB.NET express Here's my question - say I have a want to manage a list of books. Each book has an Author, Title and ISBN Now, I am used to using Arrays so I would normally do something like this: Set an array up during the init routine (called from form_load) say of
1
8119
by: John Wright | last post by:
I want to create a file hash on my exe files and store the hash signature in a database so I can retrieve the value to compare the file hash to ensure I have an untampered file. How can this be done in VB.NET 2005 john
2
1836
by: fool | last post by:
Dear group, I am a beginner in php and I was little bit experience in C language. I want to read a file's content and delete the first line of the file and display those lines which has got deleted. The content of the external file , which is in .cvs format is: -------------------------------------------------------------------
13
1703
by: sathyashrayan | last post by:
Dear group, pls go through the following function definition: function at_show_aux(parent, child) { var p = document.getElementById(parent); var c = document.getElementById(child); var top = (c == "y") ? p.offsetHeight+2 : 0; var left = (c == "x") ? p.offsetWidth +2 : 0;
8
1875
by: isaac2004 | last post by:
hello, i posted with a topic like this but got no real feedback(prob cuz of lapse in my explanation) so i am trying it again. i am trying to set up a function that brings in a txt file and adds the file into a 2d array. I have this to get the file. #include <iostream> #include <string> #include <fstream> using namespace std;
9
1616
by: blazted | last post by:
I have a text file with a list of servers and then a description of the server along with the applications that may be hosted on them. Here is a example SERVER01 SERVER01 Windows Server 2003 RC2. SQL server. Runs blah blah.. blah. and so forth. SERVER02 SERVER02 REDHAT Linux. MAIL etc etc etc I am trying to open up the file and parse it into an array that will only list the SERVER Names and then a hash that will have the SERVER...
0
8302
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.