Post Reply 
PHP Problem
Author Message
MaDc0w
need...steak....sauce.....

Posts: 255.3702
Threads: 22
Joined: 15th Apr 2007
Reputation: -6.35773
E-Pigs: 10.1397
Offline
Post: #1
PHP Problem
I need help fixing the following code.

partial code:

Code:
1
2
3
4
5
6
7
8
9
$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:
1
2
3
4
5
6
7
8
9
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.


http://www.pac-rom.com

    (__)
  <@ 0>    m0o
     \_/
06/08/2008 07:22 AM
Find all posts by this user Quote this message in a reply
MaDc0w
need...steak....sauce.....

Posts: 255.3702
Threads: 22
Joined: 15th Apr 2007
Reputation: -6.35773
E-Pigs: 10.1397
Offline
Post: #2
RE: PHP Problem
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:
1
2
3
4
5
6
7
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:
1
2
3
4
5
6
7
8
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!


http://www.pac-rom.com

    (__)
  <@ 0>    m0o
     \_/
06/08/2008 10:28 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA
Smart Alternative

Posts: 17,022.2988
Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391
E-Pigs: 446.1274
Offline
Post: #3
RE: PHP Problem
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:
1
2
3
4
5
6
7
8
$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);

(This post was last modified: 06/08/2008 05:41 PM by ZiNgA BuRgA.)
06/08/2008 05:39 PM
Visit this user's website Find all posts by this user Quote this message in a reply
MaDc0w
need...steak....sauce.....

Posts: 255.3702
Threads: 22
Joined: 15th Apr 2007
Reputation: -6.35773
E-Pigs: 10.1397
Offline
Post: #4
RE: PHP Problem
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.

http://www.pac-rom.com

    (__)
  <@ 0>    m0o
     \_/
06/08/2008 10:49 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA
Smart Alternative

Posts: 17,022.2988
Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391
E-Pigs: 446.1274
Offline
Post: #5
RE: PHP Problem
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
(This post was last modified: 06/08/2008 10:54 PM by ZiNgA BuRgA.)
06/08/2008 10:53 PM
Visit this user's website Find all posts by this user Quote this message in a reply
MaDc0w
need...steak....sauce.....

Posts: 255.3702
Threads: 22
Joined: 15th Apr 2007
Reputation: -6.35773
E-Pigs: 10.1397
Offline
Post: #6
RE: PHP Problem
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!

http://www.pac-rom.com

    (__)
  <@ 0>    m0o
     \_/
08/08/2008 04:10 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA
Smart Alternative

Posts: 17,022.2988
Threads: 1,174
Joined: 19th Jan 2007
Reputation: -1.71391
E-Pigs: 446.1274
Offline
Post: #7
RE: PHP Problem
Cool!
09/08/2008 11:26 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

 Quick Theme: