Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Neatening HTML source produced by a PHP script

Status
Not open for further replies.

chrismassey

Programmer
Aug 24, 2007
264
GB
Hello,

On a new website of mine I have a HTML page which requires/includes 2 PHP scripts, 1 which opens a txt file which lists all the Category links, and another which opens a txt file and lists all the sponsors links.

However, when I view the source code of my website, it isn't as neat as I would like it to be...

For example...

(current source):

Code:
<li><img src="Images/LittleTV.bmp"><a href="Categories.php?Category=Documentary
"> Documentary
></a></li><li><img src="Images/LittleTV.bmp"><a href="Categories.php?Category=Movies
"> Movies
></a></li>

(how I would like the source to look like):

Code:
<li><img src="Images/LittleTV.bmp"><a href="Categories.php?Category=Documentary"> Documentary></a></li>
<li><img src="Images/LittleTV.bmp"><a href="Categories.php?Category=Movies"> Movies></a></li>

By looking at the source code, after each time a variable is printed, it produces a new line afterwards. Note that the word "Documentary" and "Movie" are the products of the variable(s) that are found in the script.

My PHP script for printing the Category links is:

Code:
<?php
$path = 'Logs/CategoryLog.txt';
$lines = file($path);
foreach ($lines as $line) {
	echo '<li><img src="Images/LittleTV.bmp"><a href="Categories.php?Category=' . "$line" . '">' . " $line>" . '</a></li>';
}
?>

Note that the same issue occurred even when I used no concatenation. How can I stop this and neaten my source code?

Thanks alot,

Chris
 
i suspect this is to do with the data rather than the php. see whether this solves the problem

Code:
<?php
$path = 'Logs/CategoryLog.txt';
$lines = file($path);
array_walk($lines, 'cleanup'); //get rid of trailing line break in the lines array
foreach ($lines as $line) {
   echo <<<HTML
	<li>
		<img src="Images/LittleTV.bmp" /><a href="Categories.php?Category={$line}">$line></a>
	</li>

HTML;
}
function cleanup($var){
  return urlencode(trim($var));
}
?>
 
Hey,

Thank you very much for your response.

I tried your code above and it doesn't seem to work. Nothing is printed at all. However you have solved another issue I have wanted to know for a while hehe, which is use of echo <<<HTML/HTML; which I use in Perl scripts and couldn't find a similar method for PHP.

If you have any other suggestions they would be greatly appreciated. In case you need to know, the data in the file is simply stored like this...

Code:
One
Two
Three
Four
Five
Etc

Chris
 
My mistake,
I had accidently deleted a character from the initital path string. It has worked perfectly. The only thing I had to add was { } around the 2nd variable for each line.

Thank you very much.
 
no worries. the issue is/was that the file() command captures the line terminators. you could also have fixed the problem by adding an argument to the file() function. check out for the right combination of flags.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top