Welcome Guest, Not a member yet? Register   Sign In
Reading data from a file
#1

[eluser]Unknown[/eluser]
Hi all,

I'm currently trying to read information out of a text file. The text file looks like this:

Code:
Values =
{
   string1 = "mystring1",
   string2 = "mystring2",
   int1 = 12,
}

Is it possible to retrieve 'mystring1', 'mystring2' and the int, and store them as variables? (They will be going into a database).

Any advice is greatly appreciated.
#2

[eluser]InsiteFX[/eluser]
CodeIgniter User Guide - File Helper

InsiteFX
#3

[eluser]wh1tel1te[/eluser]
Looks like you'd have to get the contents of the .txt file into a variable, then do some major parsing on it, probably with regex. If you're allowed to change the format of the .txt file's contents, might be best if you stored the contents as a serialized array. Then you could simply grab the contents of the file then do an unserialize.
#4

[eluser]Unknown[/eluser]
Well I've found a solution - most likely not the best but it works Wink

Code:
$string = read_file('myfile.txt');

$array = preg_split('"values|=|{|}|,|\"|string1 |string2 |int1"', $string, 0 , PREG_SPLIT_NO_EMPTY);

print_r($array) //to see where the variables are in the array.

I tried using trim to remove some of the characters but it didn't seem to work at all? Either way this works just seems a rather ugly way of doing it. Sorry I'm rather new to php :/

Thanks for the replies




Theme © iAndrew 2016 - Forum software by © MyBB