1

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?

4
  • 1
    why not store actual php variables $key1='value1'; .. then use include\require Commented Oct 24, 2014 at 3:33
  • also note that the scripting code could cross many lines with many identations, my sample is just very simple example. I would like to get a general idea on how to approach this problem Commented Oct 24, 2014 at 3:34
  • 1
    generally you don't have this problem , because its not how you store variables Commented Oct 24, 2014 at 3:35
  • ok, not generally then :) is there any hackish way to do this? store a long string preserving spaces, newlines that is acrosing multiple lines. Commented Oct 24, 2014 at 3:45

1 Answer 1

2

Some alternatives:

Sign up to request clarification or add additional context in comments.

4 Comments

I vote for JSON. serialize is another possibility for the list. Any is better than rolling your own in this case.
I don't get the third option use PHP code directly and include it, note the scripting code in the configuration file is not PHP code, it is some other scripting language code. forth option parse_ini_file seems not dealing with the special sample I mentioned here
We are steering you away from the "special sample" to use another standard format or method.
I suggest heredoc for storing scripting code in PHP. Your code can be multiline and contain any special chars, quotes etc. php.net/manual/en/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.