473,231 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

[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 2128

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
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...
6
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
by: Victor Paraschiv | last post by:
I need to serialize into an XML file a hashtable. From MSDN at XmlSerializer:...
9
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...
1
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...
2
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...
13
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 ...
8
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...
9
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

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.