devWhiz Posted April 9, 2011 Share Posted April 9, 2011 So say I have a file with the contents.. ; this is a comment var2 : variable ; another comment var3 : file.txt how would I be able to do <?php echo $var2; ?> That would echo "variable" and <?php $file = file($var3); ?> So its reading the file to get the variable from the text file and if the line starts with ";" its disregarded.. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/233207-variables-inside-a-file/ Share on other sites More sharing options...
sunfighter Posted April 11, 2011 Share Posted April 11, 2011 This looks straight forward CLUEL3SS. OPEN the file READ each line and examine the line for the properties your looking for. php string functions to use are found here: http://php.net/manual/en/ref.strings.php. The only problem I see is "$file = file($var3);". Why store the text file name as a variable? FILE 3 : file.txt Maybe a better way of storing it. Hope this helps, if not just speak up. Quote Link to comment https://forums.phpfreaks.com/topic/233207-variables-inside-a-file/#findComment-1200097 Share on other sites More sharing options...
AbraCadaver Posted April 11, 2011 Share Posted April 11, 2011 If you have control over the text file and how it's formatted, you might try: http://php.net/manual/en/function.parse-ini-file.php Quote Link to comment https://forums.phpfreaks.com/topic/233207-variables-inside-a-file/#findComment-1200108 Share on other sites More sharing options...
devWhiz Posted April 11, 2011 Author Share Posted April 11, 2011 If you have control over the text file and how it's formatted, you might try: http://php.net/manual/en/function.parse-ini-file.php I notice this puts each value into an array... how would I call a certain variable? Quote Link to comment https://forums.phpfreaks.com/topic/233207-variables-inside-a-file/#findComment-1200220 Share on other sites More sharing options...
AbraCadaver Posted April 11, 2011 Share Posted April 11, 2011 Well, I would just use the array: echo $array['var2']; Or you could extract() the array and it would create the vars for you. Quote Link to comment https://forums.phpfreaks.com/topic/233207-variables-inside-a-file/#findComment-1200242 Share on other sites More sharing options...
devWhiz Posted April 11, 2011 Author Share Posted April 11, 2011 Well, I would just use the array: echo $array['var2']; Or you could extract() the array and it would create the vars for you. that worked thanks man!! This looks straight forward CLUEL3SS. OPEN the file READ each line and examine the line for the properties your looking for. php string functions to use are found here: http://php.net/manual/en/ref.strings.php. The only problem I see is "$file = file($var3);". Why store the text file name as a variable? FILE 3 : file.txt Maybe a better way of storing it. Hope this helps, if not just speak up. I appreciate your help as well ! Quote Link to comment https://forums.phpfreaks.com/topic/233207-variables-inside-a-file/#findComment-1200265 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.