file_get_contents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
themurph
Forum Commoner
Posts: 76
Joined: Wed Apr 19, 2006 1:56 pm
Contact:

file_get_contents

Post 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?
    nickvd
    DevNet Resident
    Posts: 1027
    Joined: Thu Mar 10, 2005 5:27 pm
    Location: Southern Ontario
    Contact:

    Post 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 ?>
    User avatar
    themurph
    Forum Commoner
    Posts: 76
    Joined: Wed Apr 19, 2006 1:56 pm
    Contact:

    Post 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.
    User avatar
    feyd
    Neighborhood Spidermoddy
    Posts: 31559
    Joined: Mon Mar 29, 2004 3:24 pm
    Location: Bothell, Washington, USA

    Post by feyd »

    you may want to look into the tokenizer functions.. specifically, token_get_all().
    User avatar
    waradmin
    Forum Contributor
    Posts: 240
    Joined: Fri Nov 04, 2005 2:57 pm

    Post 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
    User avatar
    themurph
    Forum Commoner
    Posts: 76
    Joined: Wed Apr 19, 2006 1:56 pm
    Contact:

    Post 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.
    Post Reply