PHP: Need help removing line break from code
I am trying to get names from a .txt file. I am using file('filename.txt') but if i use a php for loop to try and trim the names, it comes out on multiple lines:
This is the actual line that prints the javascript (NOTE: I am going to be putting these names into a select box, if you can fix the problem at hand the rest of the code should work so unless you a big problem with it you don't need to comment)
<?php
for($i=1; $i < 27; $i++){
echo "selbox.options[selbox.options.length] = new Option('".lin开发者_运维技巧es[$i]."','".$lines[$i]."');\n";
}
?>
Heres how it comes out on the browser:
selbox.options[selbox.options.length] = new Option('Djamal ABDOUN
','First1 Last1
');
selbox.options[selbox.options.length] = new Option('Chadli AMRI
','First2 Last2
');
I need those to be on one line so i dont get a unterminated string literal js error Any ideas on what i can do here?
EDIT: Oh i should probably mention, the $lines var is initiated like this: $lines = file('filename.txt'); and inside that file i have formatted like this First1 Last1 First2 Last2 First3 Last3 etc. and i hit delete after each Last name until the next first name is touching, then hit enter to put it on a new line (Editor is notepad++)
Use trim($lines[$i])
instead of $lines[$i]
since your linebreaks seem to be always at the end of the line.
The file() method has an optional parameter called FILE_IGNORE_NEW_LINES
, to avoid new lines being appended. Use it.
file
has an option to remove new lines in the result array
$lines = file('filename.txt', FILE_IGNORE_NEW_LINES);
精彩评论