Endless Paradigm

Full Version: PHP Problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need help fixing the following code.

partial code:

Code:
$rawfile = file_get_contents("E:\USERS\pcminusmulti7\www\undergroundz\file.txt"); // open file for editing
$rawfilelines = explode("\n",$rawfile, -1);  // this seems to go wierd or something
for ($i=0;$i<count($rawfilelines);$i++){     
$outputs = str_replace("images/smilies/", " ", $rawfilelines[$i]); // replace messy paths in array before we write it to file 
$f2 = fopen("E:\USERS\pcminusmulti7\www\undergroundz\file2.txt", "a"); // open new file
fwrite($f2, $outputs."\n"); // output cleaned paths to new file, output stays wierd
fclose($f2);  // and leave
echo $outputs."<br><br>"; // also shows wrong output!!!
}


content of file.txt:

Code:
images/smilies/smile.gif :)
images/smilies/wink.gif ;)
images/smilies/cool.gif :cool:
images/smilies/biggrin.gif :D
images/smilies/tongue.gif :P
images/smilies/rolleyes.gif :rolleyes:
images/smilies/shy.gif :shy:
images/smilies/sad.gif :(
images/smilies/at.gif :at:


result of file2.txt:

Code:
smile.gif :)

 smile.gif :)
 wink.gif ;)

 smile.gif :)
 wink.gif ;)
 cool.gif :cool:

 smile.gif :)
 wink.gif ;)
 cool.gif :cool:
 biggrin.gif :D

 smile.gif :)
 wink.gif ;)
 cool.gif :cool:
 biggrin.gif :D
 tongue.gif :P


If I echo the output to the active window I get the same results.
I also get the same results no matter how I try loading the data.

I fixed the problem myself. :P

Since I could not fix that bug/problem, I decided to take a different route.

I trimmed the file while creating file.txt.

Old code:

Code:
while($info = mysql_fetch_array( $data ))
{
$add = "". $info['image']. " ". $info['find']."\n";
$f = fopen("E:\USERS\pcminusmulti7\www\undergroundz\file.txt", "a");
fwrite($f, $add);
fclose($f); 
}



New code:

Code:
while($info = mysql_fetch_array( $data ))
{
$add = "". $info['image']. " ". $info['find']."\n";
$outputs = str_replace("images/smilies/", " ", $add); // replace messy paths in array before we write it to file 
$f = fopen("E:\USERS\pcminusmulti7\www\undergroundz\file.txt", "a");
fwrite($f, $outputs);
fclose($f); 
}



This of course is cleaner and the whole block of code in the
first post becomes unnecessary.

Still makes me wonder why it doesn't work, on my test server it runs fine!
Mythos tested it on his server and it also ran fine!
So? Anyone got a clue?
I figure it has to do with it running on an IIS server with
some bad/wrong/my badly configured php.ini..

You win some, you lose some..
Thanks for reading my rambling!

If I recall correctly, Windows breaks newlines with \r\n, so exploding on \n will keep all the \r's in place.

I wouldn't fopen() all the time during the loop (though if this is a personal script, shouldn't be an issue).  You should also escape \ with \\.

PHP Code:
$f = fopen("E:\\USERS\\pcminusmulti7\\www\\undergroundz\\file.txt", "a");
while($info = mysql_fetch_array( $data ))
{
$add = "{$info['image']} {$info['find']}\n";
$outputs = str_replace("images/smilies/", " ", $add); // replace messy paths in array before we write it to file
fwrite($f, $outputs);
}
fclose($f);

Well even though the script now works i did want to try your suggestions.

It's much faster without opening the file in a loop. :D

So if I wanted to use that piece of code i'd have to trim the \r's to stop what happens in file2.txt?  I need to try that when I have time.

Now all I need is MyPlaza for MyBB 1.4 and I'm all set! Wis
Actually, You should separate the username formatting as it's own plugin.  :D

Try If

* runs from ZB before he gets smacked.
MaDc0w Wrote:So if I wanted to use that piece of code i'd have to trim the \r's to stop what happens in file2.txt?  I need to try that when I have time.
Probably, but I haven't looked through all the code.
MaDc0w Wrote:Now all I need is MyPlaza for MyBB 1.4 and I'm all set! Wis
Actually, You should separate the username formatting as it's own plugin.  :D
O_o, I never knew you ran a board.  Can I see it? :P
I also noticed how picky php is to empty lines when coding. :)
And I cleaned all my paths too.

http://www.undergroundz.org

not much to see atm, all clean and new..
Just needs some content now!
Cool!
Reference URL's