Welcome Guest, Not a member yet? Register   Sign In
Read Content from a .txt file
#1

[eluser]cinewbie81[/eluser]
Hi guys,

Again, it's CI irrelevant topic again ... but i just dont know how to get it done..

I have this myfile.txt located at /var/www/myproject/myfile.txt file as following:

Code:
StudentID: S0001
StudentName: Francis Light
Age: 20
Description: Francis light is bla bla bla

Now in my Controller, i want to assign each of the value from myfile.txt to a variable .. So that i have something like :
Code:
$studentID = "S0001";
$studentName = "Francis Light";
$Age = "20";
$Desc = "Francis light is bla bla bla"

How can i read from the file and assign it to the variable ? Anyone ??

Thanks in advance
#2

[eluser]Pascal Kriete[/eluser]
I hope you're not trying to store data this way. Having it in a db would be a lot more efficient.

More to the point, CI has a file helper that will give you the content of the file (if you're using plain old php try the file_get_contents function). Then you could split the returned string at the line breaks and get your data from those. I can post an example later if you're having trouble.
#3

[eluser]cinewbie81[/eluser]
For certain reason, I have to store the data this way (just temporary) ...
I know file_get_contents will read the content of the file, but how can I split the returned string at the line breaks and get the data from those ???

any example would be appreciated .. thx
#4

[eluser]xwero[/eluser]
the easy way would be
Code:
$lines = file('/var/www/myproject/myfile.txt'); // gets file in array using new lines character
foreach($lines as $line)
{
   $temparr = explode(':'$line);
   ${$temparr[0]} = trim($temparr[1]);
}
#5

[eluser]cinewbie81[/eluser]
Cheers .. that's definitely works ..
but i was wondering what if the description contain the character ":" ??

opps i have too many question, but this might be possible !
#6

[eluser]Seppo[/eluser]
$temparr = explode(':'$line, 2); will do the trick =)
#7

[eluser]Rick Jolly[/eluser]
[quote author="inparo" date="1200502072"]I hope you're not trying to store data this way. Having it in a db would be a lot more efficient.
[/quote]
Actually, I think the file is more efficient in this case. No database operations are required.

cinewbie81, if you are able to store your data in "ini" format you could use php's parse_ini_file() which would return an associative array. Example myfile.txt in ini format:
Code:
StudentID = "S0001"
StudentName = "Francis Light"
Age = 20
Description = "Francis light is bla bla bla"
#8

[eluser]thurting[/eluser]
You may also want to take a look at Zend_Config_Ini and Zend_Config_Xml from the Zend Framework.
#9

[eluser]xwero[/eluser]
Now that formats get thrown around i will add yaml or json. Yaml shorthand looks like json.
#10

[eluser]cinewbie81[/eluser]
I take Rick's advise and use .ini solution .. The code is like following

Code:
$ini_array = parse_ini_file(/path/to/myFile.ini);

$StudentID = $ini_array['StudentID'];
$StudentName = $ini_array['StudentID'];
$Age = $ini_array['Age'];
$Desc = $ini_array['Description'];

It's works ..
Anyway, if the value of some of those field contain the "=" character (Refer to the ini file below), it give me parsing error

Here's the sample .ini file that will cause the error :

Code:
StudentID = "S0001"
StudentName = "Francis Light"
Age = 20
Description = "Francis light = is bla bla bla"

Any solution




Theme © iAndrew 2016 - Forum software by © MyBB