Thursday, January 6, 2011

php - working with files (4)

It reads a text file.
Make the first line title, and print.
Remove the line.
Using
nl1br(), converting text line breaker to the <br /> tag,
print the rest.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
 <title>Untitled</title>
</head>
<body>

<?php

// read file
$data=file('C:\wamp\www\testFolder\tetsuro\tetsuroP04_php_file3\omelette.txt')
or die('Could not read file!');


/* first line is title. read it into variable */
$title=$data[0];

// remove first line from array
array_shift($data);
?>
 
<h2><?php echo $title; ?></h2>


<?php

//
// nl2br() function converts regular text linebreaks
// into the HTML equivalent, the <br /> tag.
//


/* iterate over content and print it */
foreach($data as $line){
 echo nl2br($line);
}

?>

</body>
</html>

No comments:

Post a Comment