Jump to content

Loading Text File


wemustdesign

Recommended Posts

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-

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.