Skip to content
Advertisement

PHP file reading

I have a txt file with name source.txt and has the following content.

Name = "john"
image = "/path"
email = "mail@mail.com"

and some other data

I read and echo all the content. But I want specific data and assign to the variable. if I need name. Then read the file and select john and assign to the variable

Advertisement

Answer

Here an mini code example

test.txt :

Name = "john"
image = "/path"
email = "mail@mail.com"

exemple script

<?php
$data_array = array();
$data_array_2 = array();
$fp = @fopen("./test.txt", "r");
if ($fp) {
    while (($line = fgets($fp)) !== false) {
        array_push($data_array,explode("=",$line));
        list($key,$val) = explode("=",$line);
        $data_array_2[$key] = $val;
    }
}
fclose($fp);
echo "<pre>";
var_dump($data_array);
echo "</pre>";
echo "<pre>";
var_dump($data_array_2);
echo "</pre>";
?>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement