18

I am attempting to make a gallery that calls the image names from a flat file database using the PHP 'fgets' function. There are different sections in the gallery, each with it's own default image, and a small list of images that the users can select from. Everything is working fine, except for one button.

I have one button on the page that is supposed to reset all the galleries to their default images using Javascript OnClick. It works exactly as I want it to, with one small hitch: It copies the line break at the end of the line allong with the characters on the line, breaking the Javascript.

The offending code:

function back(){
document.getElementById('back').className='back';
document.getElementById('one').className='cellcont';

//This should output the proper javascript, but does not
<?php
$a = fopen('c.txt','r');
if (!$a) {echo 'ERROR: Unable to open file.'; exit;}
$b = fgets($a);
echo "document.getElementById('i1').src='$b';";
fclose($a);
?>

}

How it outputs:

function back(){
document.getElementById('back').className='back';
document.getElementById('one').className='cellcont';
document.getElementById('i1').src='00.jpg
';}

As you can see, the ending quotation mark and the semi-colon falls on the next line, and this breaks the button.

With the files I'm using now, I can get around this problem by changing, "fgets($a)" to, "fgets($a, 7)" but I need to have it grab the entire line so that if the client decides to enter a file with a longer name, it does not break the gallery on them.

Shreger
  • 183
  • 1
  • 1
  • 5
  • Since it's a string, you're just trying to remove whitespace (linebreak) characters in a string. Lots of answers around here for that, I imagine. – Jared Farrish Sep 19 '11 at 23:00

3 Answers3

40

Use rtrim().

Specifically:

rtrim($var, "\r\n");

(To avoid trimming other characters, pass in just newline.)

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ariel
  • 25,995
  • 5
  • 59
  • 69
  • If you have to handle files with fixed-length records and trailing spaces, this answer is the one. The function provided by @Alex Kennberg, even though correct for the current question, was exactly the reason for a bug I just fixed. – Bizarro Nov 11 '16 at 17:57
  • A weird thing with this function: usually PHP reads either " " or ' ' as a string. I generally use ' '. However, you must use quotes " " for this function. I guess in this case PHP decides to read ' ' as a single character instead of as a string. Tested on a local Windows server. – Josh Powlison Nov 19 '18 at 15:11
28

Your best bet is to use the php trim() function. See http://php.net/manual/en/function.trim.php

$b = trim(fgets($a));
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
Alex Kennberg
  • 870
  • 6
  • 13
  • 4
    be careful, it will also remove some other characters such as spaces that may be part of the actual file content you want to read. – MikaelW Feb 26 '15 at 20:48
  • 2
    This is sort of wrong. The answer by @ariel should be the accepted one as it will only remove the trailing new line, and not all whitespace at both ends of the line. – Svish Feb 26 '16 at 13:29
  • In context of the entire question this is correct. If your input requires to preserve trailing white space then you need a stricter use of trim, but when you are getting a list of URLs you do not want the extra space. – Alex Kennberg Feb 27 '16 at 14:43
0
$b = fgets($a);
$b = preg_replace("/[\n|\r]/",'',$b);
Korvin Szanto
  • 4,531
  • 4
  • 19
  • 49