I'm pretty new to PHP, so don't flame me if I slip something up 🙂
Your 4th line isn't necessary because you do the same thing in your while loop. But it seems like you treat $file as an array; it's a string. So the 3rd line doesn't do anything (you reassign $file later, which overwrites the array), and the 7th line just returns 1, which isn't good for much. You also don't need lines 8, 9 and 18, 16, or 17. On line 10, the substr function returns a string; that string should be assigned to a variable to be used later. I'd suggest putting line 12 before line 10 and assigning the value of the function of line 10 to $file. I assume you have some Javascript function launchwindow defined elsewhere. But I think the last argument should have an opening quote. Then, I think it should all work.
Here's your code with my modifications:
if ($id == "14") {
$path = 'test';
$dir = opendir($path);
while ($file=readdir($dir)) {
if ($file != "." && $file !="..") {
$badStr = "$file";
$file = substr("$file", 0, -4);
$goodStr = "/<a href=\"javascript:launchwin('$path/$file.jpg','newwindow','left=22,top=22,scrollbars=yes')\">$file</a>/";
$MyStr = str_replace($badStr, $goodStr, $MyStr);
}
}
closedir($dir);
}
print $MyStr;