TobesC Posted September 6, 2007 Share Posted September 6, 2007 When I'm creating sets of links seperated by " | ", while reading out of a text file line by line, php attaches one more character onto the hyperlink. instead of getting "hyperlink I get hyperlink . any ideas? im using the fgets function. thanks Link to comment https://forums.phpfreaks.com/topic/68289-extra-characters-after-a-while-loop/ Share on other sites More sharing options...
btherl Posted September 6, 2007 Share Posted September 6, 2007 Please post your code Link to comment https://forums.phpfreaks.com/topic/68289-extra-characters-after-a-while-loop/#findComment-343354 Share on other sites More sharing options...
TobesC Posted September 6, 2007 Author Share Posted September 6, 2007 <?php $file = fopen("places.txt", "r"); while(!feof($file)) { $i=$i+1; ?> <?php if ($p!=$i) { ?><a href="index.php?p=<?php echo $i; ?>"><?php } ?><?php echo fgets($file); ?><?php if ($p!=$i) { ?></a><?php } ?> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/68289-extra-characters-after-a-while-loop/#findComment-343358 Share on other sites More sharing options...
btherl Posted September 6, 2007 Share Posted September 6, 2007 Try replacing fgets($file) with trim(fgets($file)) and see what you get. When you read lines from a file, they include the newline character. trim() will remove that for you. Link to comment https://forums.phpfreaks.com/topic/68289-extra-characters-after-a-while-loop/#findComment-343360 Share on other sites More sharing options...
TobesC Posted September 6, 2007 Author Share Posted September 6, 2007 thanks - that solved the problem, but only for half of the links (?). heres the other set that im creating. same problem, extra character after the word. i included the "trim" also. <?php $file = fopen($p.".txt", "r"); while(!feof($file)) { $j=$j+1; ?> <?php if ($a!=$j) { ?><a href="index.php?p=<?php echo $p; ?>&a=<?php echo $j; ?>&r=<?php echo $r; ?>"><?php } ?><?php echo trim(fgets($file)); ?> <?php if ($a!=$j) { ?></a><?php } ?> | <?php } ?> Link to comment https://forums.phpfreaks.com/topic/68289-extra-characters-after-a-while-loop/#findComment-343365 Share on other sites More sharing options...
btherl Posted September 7, 2007 Share Posted September 7, 2007 There is probably some other hidden character that trim() doesn't deal with. Try this diagnostic script: <?php $file = fopen($p.".txt", "r"); $j = 0; while(!feof($file)) { $j++; print "Line $j: " . urlencode(fgets($file)) . "<br>"; } ?> What that will do is it will show you the ascii values of any invisible characters. After running this, post an extract of the output (or full output if it's small) and we can see what needs doing. For this script, don't use trim(). Link to comment https://forums.phpfreaks.com/topic/68289-extra-characters-after-a-while-loop/#findComment-343437 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.