Forum Moderators: coopster

Message Too Old, No Replies

Variables from text page

How do I do it?

         

Bonusbana

4:25 pm on Nov 18, 2004 (gmt 0)

10+ Year Member



I have this text in a text file:

$a1 = #123text#
$a2 = #12345
texttest
text#

Does anybody know a quick way to open the text file in php and give the proper variables their value like:

echo $a1; should return "123text" and echo $a2; should give me "12345texttesttext"

Anyone?

thanks

coopster

5:58 pm on Nov 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have a look at the eval() [php.net] function.

mincklerstraat

6:42 pm on Nov 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(oh how I cringe when I see eval())
You can try this:

$text = file_get_contents('textfile.txt');
$check = preg_match_all('/$([\w])+[\s]*=[\s]*#([^#]+)#/', $text, $matches);
foreach($matches[1] as $k => $v){
/* if you want to get rid of line endings as in example */
$matches[2][$k] = str_replace('\n', '', $matches[2][$k]);
$$v = $matches[2][$k];
}

If you're on windows or mac, you might also str_replace for \r\n or \r\n\r or whatever the line endings are for those platforms. Test before you deploy (you're smart enough to do that - more here as a 'general disclaimer') - I haven't tested it.