Prev | Current Page 357 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

txt", "rt");
// While the EOF isn't reached, read in another line and output it
while (!feof($fh)) echo fgets($fh);
// Close the handle
fclose($fh);
?>
Stripping Tags from Input
The fgetss() function operates similarly to fgets(), except that it also strips any
HTML and PHP tags from the input. Its prototype follows:
string fgetss(resource handle, int length [, string allowable_tags])
If you??™d like certain tags to be ignored, include them in the allowable_tags parameter.
As an example, consider a scenario in which contributors are expected to submit
their work in HTML format using a specified subset of HTML tags. Of course, the
authors don??™t always follow instructions, so the file must be filtered for tag misuse
before it can be published. With fgetss(), this is trivial:
// Build list of acceptable tags
$tags = "

";
// Open the article, and read its contents.
$fh = fopen("article.html", "rt");
while (!feof($fh)) {
$article .= fgetss($fh, 1024, $tags);
}
CHAPTER 10 ?–  WORKING WITH T HE FILE A ND OPERATING SYSTEM 295
// Close the handle
fclose($fh);
// Open the file up in write mode and output its contents.
$fh = fopen("article.html", "wt");
fwrite($fh, $article);
// Close the handle
fclose($fh);
?>
?– Tip If you want to remove HTML tags from user input submitted via a form, check out the strip_tags()
function, introduced in Chapter 9.


Pages:
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369