Page 1 of 1

file_get_contents

Posted: Sat Aug 12, 2006 8:12 pm
by themurph
I am trying to use file_get_contents() to read in an actual php script and then parse
that script in order to make on the fly manipulations to the script, and eventually
save it out. In other words I am using a php script to analyse/edit/create another php
script.

My script seems to be stumbling on the <? and ?> tags. just to test, I am simply trying to
echo out each line of the script to the browser. The <? was not being echo'd at all, though
the ?> was. I made the modification below to simply try and ignore the tags altogether,
but it is still writing out a ?> tag.

Here is my code:

Code: Select all

<?php
$lines = explode("\n", file_get_contents('test.php'));
foreach ($lines as $line)
  {
     trim($line);
     if ($line == "<?") { continue; }  // does not appear to work
     if ($line == "?>") { continue; }  // does not appear to work
     print "$line<br>";
  }
?>
Hope this made sense.

Im assuming the php engine is simply interpreting the opening and closing tags in the string
strangely? How can I circumvent this?

    Posted: Sat Aug 12, 2006 8:16 pm
    by nickvd

    Code: Select all

    $lines = file('test.php');
    .. re-reading your post, this wont solve your problem, however it will eliminate your explode() call...

    I would use file_get_contents and then regular expressions to extract whatever is inbetween <?php and ?>

    Posted: Sat Aug 12, 2006 8:19 pm
    by themurph
    same result using the file() function in lieu.

    The opening tag is ignored and the closing tag is written.

    And neither tags are parsed in the if() when trying to ignore them.

    Posted: Sat Aug 12, 2006 8:38 pm
    by feyd
    you may want to look into the tokenizer functions.. specifically, token_get_all().

    Posted: Sat Aug 12, 2006 9:35 pm
    by waradmin
    If I understand what your doing, your trying to edit the contents of a php file with a php file.

    If so, while learning the basics of PHP I created some blog software. In the blog software was a config.php file. I wanted the user to be able to edit the config variables without having to open the file in an extrernal source, so i made the first file (called EDIT_CONFIG.php) which contained:

    Code: Select all

    <?php
    include("validate.php"); //the security check for the session
    $filename = "../config.php";
    $fp = fopen($filename, "r") or die("Can not open $filename");
    ?>
    <center><form action="update_config.php" method="post" name="ipedit">
    <textarea name="ip" cols="100" rows="25" id="ip">
    <?php while (!feof($fp)) {
    	$line = fgets($fp, 1024);
    	echo "$line";
    	}
    ?>
    </textarea><br>
    <input type="submit" name="submit" value="submit">
    </form></center>
    </body></html>
    Then update_config.php looked like so:

    Code: Select all

    <? include('../../config.php'); ?>
    <?php
    $ip = $_POST['ip'];
    $submit = $_POST['submit'];
    # $_POST = stripslashes($ip);
    unlink("../config.php");
    touch("../config.php");
    $filename = "../config.php";
    $fp = fopen($filename, "w") or die("Could not open $filename");
    fwrite($fp, stripslashes("$ip"));
    fclose($fp);
    echo "Updated, please <a href=\"configure.php\">click here</a> to go back";
    ?>
    Which worked perfectly for the situation I was working in. Again, this is a noob way to do it at best, but it worked for me.

    -Steve

    Posted: Sat Aug 12, 2006 9:52 pm
    by themurph
    Thanks for the suggestions, guys :D

    The latter lead me to simply use the htmlentities() function, which worked like a charm for displaying the tags.

    The former looks like it will be just what I need for the meat of my script, which will do the manipulation.