call a file and print content does not work, can anyone help?

Discussion in 'PHP' started by greenworld, Aug 27, 2006.

  1. #1
    I want to call a file, and print its contents,
    in the file I want to have some text seperated by a delimiter (%% for example: AAAA%% BBBB%% CCCC%% DDDDDDD%% and so on...)
    whatever I try does not work, anyone can help me out please?

    here is what I have:

    $data = file_get_contents("file1.csv");
    $data = explode('%%', $data);
    $printme .= "\n" . $data;
    echo $printme;


    the above code does nothing, no error either.

    thanks
     
    greenworld, Aug 27, 2006 IP
  2. Alga

    Alga Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    explode() returns an array.
    To print out $data use print_r() function:

    print_r($data);
     
    Alga, Aug 27, 2006 IP
  3. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for reply,
    you mean to have it this way:


    $data = file_get_contents("file1.csv");
    $data = explode('%%', $data);
    print_r($data);

    it did not work,
     
    greenworld, Aug 27, 2006 IP
  4. fscripting

    fscripting Banned

    Messages:
    288
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    First Test The Code

    if it opens the file or no
     
    fscripting, Aug 27, 2006 IP
  5. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks for reply;

    I am not sure I follow you,
    How can I open the file?
     
    greenworld, Aug 27, 2006 IP
  6. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I run this script and the only thing is printed is "array" without quotes,

    <?php

    $data1 = file_get_contents("file1.csv");
    $data1 = explode('%%', $data1);

    $printme .= "\n" . $data1;
    echo $printme;

    ?>
     
    greenworld, Aug 27, 2006 IP
  7. fscripting

    fscripting Banned

    Messages:
    288
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    y do u use %% use "|" instead

    atleast for test try that
     
    fscripting, Aug 27, 2006 IP
  8. Litewebsite

    Litewebsite Guest

    Messages:
    26
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Like Alga mentioned try the print_r function to print the array data. file_get_contents() returns the file as a string, if we simulate this with the following php code

    
    <?php
    $fileSimulation = '%% for example: AAAA%% BBBB%% CCCC%% DDDDDDD%%';
    $data = explode('%%', $fileSimulation);
    print_r($data);
    ?>
    
    PHP:
    It will print this
    
    Array ( [0] => [1] => for example: AAAA [2] => BBBB [3] => CCCC [4] => DDDDDDD [5] => )
    
    PHP:
    If it doesn't it might be as fscripting mentioned that file might have failed opening. If the file_get_contents() failes it will return false.
     
    Litewebsite, Aug 27, 2006 IP
  9. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    thank you for your responds,

    of course I understand that the code I have is for string,
    the code that you offerd worked very well and it showed what you posted, but it does not work with what I want to do,

    I don't want to have my data as some value in a variable,
    I simply want to get my data from a file, that is why I use file_get_contents() function,
    and I dont want to print all of the data at once, every piece of data in between %% should be printed seperatly, but this is not the issue here, for now if it prints the data, I believe i have solved my problem.
    but I don't know wha I do wrongly that it does not work,

    fscripting I have alreay tried with | sign, it does not make any different,

    thanks
     
    greenworld, Aug 27, 2006 IP
  10. fscripting

    fscripting Banned

    Messages:
    288
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Do u wanna do something like this

    eg - AAAA%% BBBB%% CCCC%% DDDDDDD%%

    u wanna print "ccccc"
    and rest u dont wanna print rite

    if u wanna do like that

    try to print "
    print_r($data[2]);"

    as it is the third value
     
    fscripting, Aug 27, 2006 IP
  11. greenworld

    greenworld Peon

    Messages:
    65
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    thanks for reply fscripting ;
    yes I know that,
    well, I have a plug in for wp blog, it does count my post (whenever there is one), and after a certain number of words it suppose to add a some small text from a file, the first count it will add AAAA the second count it will add BBB and so on... it is sequential and when it reaches to the end it will start again.
    it is very easy to handle a file of data seperted by a delimiter, you can take some text out or add one easly and reupload the file, but with variable in a php file it is not easy to do,

    thanks
     
    greenworld, Aug 27, 2006 IP