wemustdesign Posted January 28, 2010 Share Posted January 28, 2010 Hi, I am just wanting to load some variables from a text file. My text file looks like: myContent = This is some example content How can I echo myContent in PHP? Cheers Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted January 28, 2010 Share Posted January 28, 2010 Hmm, it is relatively easy, as long as your text file conforms to certain formatting rules. You will need a rule to seperate each variable, either by a new line, or by a specific unique character. An Example Text File: MyContent = This is some interesting text, with no "newlines". MyContent2 = For example, This variable will only result in this line being displayed, This line will be ignored. You can also enclose strings in quotes or other special characters that wont be used inside the actual string, or even another way is to encode the string into hexadecimal characters (No special chars will be shown, even newlines should turn into a number or character). ---- By far though, the easiest, is to use and inline include file written in php: <?php $MyContent = "..."; ?> -- So, your first step is to figure out the formatting you need to work with (if your given the text files), or create some formatting (if you made the text-files), or go a much easier and quicker route of inline PHP (if you dont need them to be actual text files). -CB- Quote Link to comment Share on other sites More sharing options...
oni-kun Posted January 28, 2010 Share Posted January 28, 2010 Something like so: Example file contents, foo.txt: MyContent = foobar Thiscontent = so and so My other content = this $filecontents = file_get_contents('foo.txt'); $array = explode("\n", $filecontents); foreach ($array as $lines) { echo $lines . "<br/>\n"; //Display all lines } //Or.. $third_entry = explode(' = ', $array[2]); //2 = 3'rd line, aka "My Other Content" echo "The third entry title is: "$third_entry[0] . " and contains the line: " . $third_entry[1] ; Should be more what you're wanting to do, don't know if the code works 100% but it's an example. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted January 28, 2010 Share Posted January 28, 2010 Although this function is for parsing configuration files. You could use the parse_ini_file function if your text file is in this format myContent = Hello, World anotherLine = Testing 123.. oneMoreLine = 'A multi-line something here' justForLuck = whatever here With the following code $line = parse_ini_file('yourfile.txt'); You can easily display a certain line using the $line variable, eg echo $line['myContent'] // first line echo $line['justForLuck'] // last line etc Quote Link to comment 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.