Include text file (I dont want it to echo/print) help plz

Discussion in 'PHP' started by cornetofreak, Jun 18, 2008.

  1. #1
    hey im trying to include a text file for an ajax chat room but its printing the content from the text file

    i dont want it to echo / print

    	require_once ROOT_DIR.'/chat/w.php';
    	require ROOT_DIR.'/chat/chat.txt';
    PHP:
    i dont want to rename it to php because the ajax is built on the text file :)

    Please help
     
    cornetofreak, Jun 18, 2008 IP
  2. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i thought that

    
    <?php
    include('textfile.txt');
    ?>
    
    PHP:
    would work without echoing or printing it. but im not sure. :S
     
    X.Homer.X, Jun 18, 2008 IP
  3. Dondon2d

    Dondon2d Peon

    Messages:
    3,193
    Likes Received:
    146
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It would print out the contents of the text file.
     
    Dondon2d, Jun 18, 2008 IP
  4. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hm, good thing this thread was made, i learned something tonight =]
     
    X.Homer.X, Jun 18, 2008 IP
  5. melol2

    melol2 Active Member

    Messages:
    511
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #5
    simple. in the txt file do this

    <?
    php stuff here
    ?>

    ajax

    <?
    php stuff here
    ?>
     
    melol2, Jun 18, 2008 IP
  6. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    nope it dont work and all thats it the text files is well plain texts thats used to pull the chat contents ie

    Robert&nbsp;&nbsp;&nbsp;&nbsp;| test<br>
    Robert&nbsp;&nbsp;&nbsp;&nbsp;| test<br>
    ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| test<br>
    hello&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;| dc sdf df <br>


    But its being echoed at the top of the page aswell in the chat i only want the ajax from the chat to pull the text not the index its self but it must be included in the index for the chat to find it :(
     
    cornetofreak, Jun 18, 2008 IP
  7. melol2

    melol2 Active Member

    Messages:
    511
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #7
    You could always put it in script tags and use that as a variable.

    
    <script>
    var read = new XMLHttpRequest();
    var sURL  = "text.txt";
    
    read.open("GET",sURL,false);
    read.send(null)
    var text= read.responseText;
    </script>
    
    Code (markup):
    maybe not the best solution but it works.

    put the text in text.txt and whenever you need it just use the variable "text".

    If you need it in other directories, just use an absolute path such as "var sURL = 'http://www.example.com/text.txt'"
     
    melol2, Jun 18, 2008 IP
  8. Plazey

    Plazey Member

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #8
    Melol2 has a good answer to it. Or you could use the function file_get_contents($url); if you need it to be in your PHP.
     
    Plazey, Jun 18, 2008 IP
  9. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #9
    $string = get_include_contents('somefile.php');
    
    function get_include_contents($filename) {
        if (is_file($filename)) {
            ob_start();
            include $filename;
            $contents = ob_get_contents();
            ob_end_clean();
            return $contents;
        }
        return false;
    }
    PHP:
    Any good?
     
    Danltn, Jun 19, 2008 IP