Reading From Files

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

Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

is that even possible?
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

surely someone knows how this can be done?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Well I don't have time to write a working script but off the top of my head I might start by reading everything into an array using file(). I'd then look into using in_array() to find the bit that I want to change. fwrite would then look to be an obvious choice for writing everything back. This is where I'd probably start having never done this before (more of a database kinda girl myself) but it may not be my final solution. I'd check out the [url=http://www.php.net/manual/en/ref.filesystem.php]filesystem functions and play with those if I was you.

Mac
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

yeah i have to do it using files because i dont have access to the database(if they even have one) of the site this is for.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

you could easily either:

a) if the content you want changed is on it's own line, to what mac said

b) read the file into a string using fread, and use preg_replace() eregi_replace() ereg_replace() or str_replace() to replace the contents.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

so if i searched the file for a specific piece then i could use hobgoblins original post to have it remove/replace all of the sections that match that pattern or something?
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

it may seem like im having you guys make my site for me but these things i have been asking are only pieces of my whole code, and some of the solutions worked for me some don't so i am still trying to getmy last question fixed. after i figure that out then all i should have left is to find out how i can have it save the information to a file at a certain time each day.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

Code: Select all

<?php
$file = file("http://");
$filename = "chart.txt";
$fe = fopen ($filename, "a+");
$ff = fread ($fe, 70000);
$userfile = "users.txt";
$users = fopen ($userfile, "r");
$userread = fread ($users, 100);
$table = "</TABLE>";
$date = date('\T\o\d\a\y \i\s l F j, Y, g:i A');
$a = 64;
$b = $userread;
$sub = 64 + 196;
$sub2 = $b + 2;
$final = $sub2 * 14 + $sub;

include 'style.css';
include 'header.php';
print "<TABLE width=100% border bordercolor='#009999' cellspacing='0'>";
while(list($array) = each($file))&#123;

if ($array > $a and $array < $final)&#123;
print $file&#1111;$array];
&#125;
&#125;
print $table;
print "<center>$date</center>";
?>
while(list($array) = each($file)){

if ($array > $a and $array < $final){
print $file[$array];

right after the if ($array > $a and $array < $final)
i want to replace some lines from that array with different ones, and id like to do it with a pattern so that every line that matches my pattern will be replaced with what i choose before i print them and then print it after everything is replaced to produce the final output.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

ok so i managed to replace the lines with ereg_replace() but i cant figure out a way to specify a limit like the if ($array > $a and $array < $b); thing so that i can make all the lines past a certain point get replaced and the rest stay the same. i also have to find a way to do that because preg_replace doesnt appear to be working as it either replaces every single line with the code, or gives me a '(' unknown modifier error message. (and yes i have been looking in the manual but there are so many functions that its hard to find soemthing that will do what you want it to) and i have tested eregi as well and it seems to work too.
Post Reply