SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    writing php code to file?

    Is it possible to write actual php code to a file? I need to be able to update a php coded array from data I parse from another website. Is it possible to write the actual code without writing it's values?

  2. #2
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

  3. #3
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you just need to escape stuff ... eg
    PHP Code:
    <?
    $str 
    '<' '?$var = ' $variable '?' '>' ;
    //or//
    $str '<' "?\$var = $variable?" '>' ;
    fputsfopen'file.php' 'w' ) , $str ) ;
    ?>
    note that I broke up the '<?' tags to '<' . '?' purely because some editors get confuddled if you don't .

  4. #4
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, not sure if I understand. I'm looping through results and displaying as

    PHP Code:
    $array_name['Data']['Items']['Type'][$type][$name]['Produces'][$n] = $result[$i]; 
    And I want to write it to the file as is, code and all.

  5. #5
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you lost me as well ... you are assigning not displaying , if you want to save the string above exactly as is to file ...

    PHP Code:
    <?
    while($whatever){

    $str .= '$array_name[\'Data\'][\'Items\'][\'Type\'][$type][$name][\'Produces\'][$n] = $result[$i];'."\n";
    }
    fputs$fp $str );
    ?>

    but if you want to substitute the dynamic stuff

    PHP Code:
    <?
    while($whatever){
    $str .= '$array_name[\'Data\'][\'Items\'][\'Type\'][' $type '][' $name '][\'Produces\'][' $n '] = ' $result[$i] . ';'."\n";
    }
    fputs$fp $str );
    ?>

  6. #6
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works perfectly, thanks.

  7. #7
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to look at building a replica of the final array you want to see and then calling serialize. I usually do this sort of thing with a template. Something like:
    PHP Code:
    define ('DATA_TEMPLATE', <<<EOS
    <?php
    \$%unserialize('%s');
    ?>
    EOS
    );

    $content = sprintf(DATA_TEMPLATE
        ,'var_name'
        ,serialize($array_to_store)
        );
    This technique is very efficient for caching data because you can then just include the file in a later program.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  8. #8
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure I understand. You mean I can just store the final array $array_name?
    How would I do that with my above code?
    Can it be saved to file then used like a normal array?

  9. #9
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    off the top of my head (i.e. not tested)

    PHP Code:
    $test_array = array();
    $test_array['some']['complex']['key'] = 'value';
    $test_array['some']['complex']['otherkey'] = 123;
    //etc

    $fh fopen('test.php''w');
    $content sprintf(DATA_TEMPLATE'new_var'serialize($test_array));
    fwrite($fh$content);
    fclose($fh); 
    and in some other file...
    PHP Code:
    include 'test.php';
    //now new_var has the same value at test_array
    var_dump($new_var); 

  10. #10
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, all that did was write a file with the "DATA_TEMPLATE" as the only content.

  11. #11
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need the define from post #7
    Edit:


    PHP Code:
     define ('DATA_TEMPLATE', <<<EOS
    <?php
    \$%unserialize('%s');
    ?>
    EOS
    );
    you might try adding
    PHP Code:
    error_reporting(E_ALL); 
    as the first line of your script as well (would have helped you catch this))

  12. #12
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I add the define I get.

    Parse error: parse error, unexpected T_SL in C:\FoxServ\www\production\test_array3.php on line 5

  13. #13
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what I tested. The only change I made was to add a "cache/" path to the file in order to get it to a web server writeable directory.

    PHP Code:
    <?php
    error_reporting
    (E_ALL);

    define ('DATA_TEMPLATE', <<<EOS
    <?php
    \$%s = unserialize('%s');
    ?>
    EOS
    ); 

    $test_array = array();
    $test_array['some']['complex']['key'] = 'value';
    $test_array['some']['complex']['otherkey'] = 123;
    //etc

    $fh fopen('cache/test.php''w');
    $content sprintf(DATA_TEMPLATE'new_var'serialize($test_array));
    fwrite($fh$content);
    fclose($fh); 

    include 
    'cache/test.php';
    //now new_var has the same value at test_array
    var_dump($new_var);
    produced:
    Code:
    array(1) {
      ["some"]=>
      array(1) {
        ["complex"]=>
        array(2) {
          ["key"]=>
          string(5) "value"
          ["otherkey"]=>
          int(123)
        }
      }
    }
    on my system.

    Also, here is the file actually produced:
    Code:
    $ cat cache/test.php
    <?php
    $new_var = unserialize('a:1:{s:4:"some";a:1:{s:7:"complex";a:2:{s:3:"key";s:5:"value";s:8:"otherkey";i:123;}}}');
    ?>
    HTH

  14. #14
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, there is something wrong with this syntax..
    PHP Code:
    define ('DATA_TEMPLATE', <<<EOS 
    <?php 
    \$%unserialize('%s'); 
    ?> 
    EOS 
    );
    It drops out of php and the rest of the script is fubared. Is something being added by the forums?

  15. #15
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are missing the \ before the $. See the previous posts

  16. #16
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have it. Still drops the script out of php.

  17. #17
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure the last EOS is the only thing on the line, especially with no whitespace in front of it? PHP is picky about how the heredoc ends.

  18. #18
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suppose you could change the line to:
    PHP Code:
    define('DATA_TEMPLATE',
    "<?php\n\$%underialize('%s');\n?>\n");
    to avoid the heredoc if you are having problems.

  19. #19
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, tha ?> still drops the rest of the script out of php.

  20. #20
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even if you cut and past from the script in post #13? What version of php and OS are you running?

  21. #21
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I cut it from post 13. doesn't work in php 4.23 apache, win 2000 and it deosn't work on php 4.33, apache, linux. Even in my IDE I can see the code drop out of php and turn black instead of highlighted. Usually a sign of something amiss.

  22. #22
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works find on my PHP 4.3.0 Apache 1.3.27 linux. Have used similar on HP-UX.

    Funny how post #13 correctly shows the syntax highlighting with the ?> as part of the heredoc string, whereas your code in #14 shows it dropping out. Not sure I can help you further.

  23. #23
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, thanks anyway. At least the original problem was solved.


Bookmarks

Posting Permissions

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