简体   繁体   中英

PHP read in multiple lines from text file

I have a existing PHP code to parse through a text configuration file and store each input line as key => value pair in array, for example text file:

key1 value1
key2 value2
key3 '

if ( foo) 
    blahblah;
else
    blaba;

';

so key1 value1 and key2 value2 can be easily parsed, but key3 value is actually single quoted multiple line of scripting code.

my existing code is something like

while (!feof($fd)) {
    $buffer = fgets($fd, 4096);
    $temp = explode(' ', $buffer);
    $array[$temp[0]] = $temp[1];
}

but how do I parse through the key3 and value a scripting code that is crossing multilines with indentation. once I get the intended input value, I would like also to be able to output in HTML form preserving the script code format, this seems non-trivial to do, looked preg_match to match multiple lines, but I think fgets only get one line of input at a time , how do I use preg_match to do that? anyone got any idea?

Some alternatives:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM