Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    144
    Thanks
    3
    Thanked 20 Times in 20 Posts

    Loading variables from a text file

    Hi Guys,

    I am trying to load a series of variables from a text document for use within a web page. The text document shows them as a list, ie.

    $variable_a=yes it works;
    $variable_b=working also;
    etc. etc.

    From here I want to call the variables inside the main webpage. So I have tried pulling the variables from the text document using include but tht displays them at the top of the page in a single line. All I want is for them to be included in the php so that further down when I call the variable it displays "yes it works" as the result! I must usea txt file as it has a backend being edited by novices who I dont want to change this main page!

    Any help is much appreciated!!

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Your pages must be .php (instead of .html), unless you specify that in an .htaccess file.

    Below are some basic methods ... different ways to include text and PHP scripts ...


    Copy and paste this into notepad, save it as "test.php" and upload it to your website.
    See what happens ...
    PHP Code:
    <?php

    $variable_a
    ="yes it works";
    $variable_b="working also";

    // or include a PHP script

    // now, the normal HTML stuff
    ?>
    <html>
    <body>
    blah blah blah<br>

    Does it work? <?=$variable_a?> <br>

    </body>
    </html>

    This is more close to what you are attempting ... to include an external .txt file into your web page ...

    Save some text in a file called "mytext.txt" ....
    Now is the time for all good men to come to the aid of their country.

    Then try this script ... call it "test2.php" ...
    PHP Code:
    <html>
    <body>
    blah blah blah<br>

    <?php include("mytext.txt");?>

    </body>
    </html>

    Maybe you want to include some random lines of text from a remote text file.
    Try this Javascript on your website:

    <script type="text/javascript" src="http://www.catpin.com/ponder.php"></script>

    (It's a random quote from my website)

    The PHP script that is called, sends back the text using a Javascript content-type ...
    Header("content-type: application/x-javascript");




    .
    Last edited by mlseim; 12-01-2010 at 04:25 AM.

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,026
    Thanks
    15
    Thanked 240 Times in 240 Posts
    Take a look at this: http://us2.php.net/manual/en/function.file.php

    It converts a plain text listing into an array. You may have to use trim() to remove the end of line characters and any end whitespace. It's about the best bet for using .txt the way you want.

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    If it has to be a text file for security, how about parse_ini_file()?

    http://php.net/manual/en/function.parse-ini-file.php

    Instead of having the variables specified as:

    $variable_a = "blah";

    just have:

    variable_a="blah"
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    the extension is irrelevant unless you call as an absolute url ... e.g

    #vars.txt
    PHP Code:
    <?$var1='blah';?>
    PHP Code:
    include('stuff/vars.txt');
    echo 
    $var1;  // will work

    include('http://blah.com/stuff/vars.txt');
    // wont work 
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •