Page 1 of 1

[SOLVED]What's wrong?

Posted: Sat Oct 16, 2010 2:01 pm
by shadowx360
Can anyone tell me what's wrong with my code?

Code: Select all

<?php
$IDs = preg_split("/[\s]*[,][\s]*/", $_REQUEST['IDs']);
$myFile = "IDs.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
for ( $counter = 0; $counter < count($IDs); ++$counter) 
{
    fwrite($fh, $IDs[counter]."\n");
    echo $IDs[counter]."\n";
}
fclose($fh);
?>
What I'm trying to do is make a PHP file that will take a parameter like : http://www.mysite.com/log.php?IDs=1,2,3 and then add to the ID.txt "1\n2\n3\n" (the \n meaning new line).

Solved

Re: [SOLVED]What's wrong?

Posted: Sat Oct 16, 2010 2:16 pm
by John Cartwright
What a useless preg_split pattern :D Although it's impossible to tell whats wrong with your script if you don't tell us the expected and actual behavior. I.e., debugging information.

A simpler version:

Code: Select all

$myFile = "IDs.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, str_replace(',', PHP_EOL, $_REQUEST['IDs']) . PHP_EOL);
fclose($fh);