PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




recode_file

Name

recode_file — Using recode as a filter, transfers the content of input_file into output_file .

Synopsis

boolean recode_file([. . .], input_file, output_file);
string . . . (optional): recode request (expressed as char_set_from..char_set_to)
resource_pointer input_file: File handle for the input file
resource_pointer output_file: File handle for the output file

Returns

TRUE on success; FALSE if the recode request fails or the file handles were not usable

Description

recode_file() acts as a filter between two file handles - reading all of the content from the current offset of the file position indicator for input_file , filtering it through recode, and writing it to output_file at the current offset of the file position indicator. Both file handles must be opened by calls to fopen() . input_file must have been opened in a mode that allows reading, while output_file must have been opened in a mode that allows writing.

The recode argument should consist of the name of the character set from which to convert, followed by two periods and the name of the target character set; for example, to convert from ASCII to base 64, the argument is "ASCII..64".

If file handle is unusable or the recode request cannot be handled by recode, the function returns FALSE .

Caution

This function may return FALSE in some situations where it successfully recodes the character set.



Version

PHP Version: 3.0.13+, 4.0RC1+

See also

To recode a single string

recode()

recode_string()

Other functions are available for building internationalized applications in PHP:

see the GNU Gettext functions.

Example

Example 1119. Fetch a news story from a web page and strip all the accents out of the text

Note

A warning such as recode: Ambiguous output in step `ISO-8859-1..ASCII-BS' may be generated by this code. This error indicates that the file contains a mix of UNIX-style (\n) and Windows-style (\r\n) line endings. The recode should work properly despite the error.



<?php
# Set the URL of the site to retrieve data from
$base_URL = "http://example.com/new_stories";

# Make a numeric date string (ie. 20010915) for today
$date = date ('Ymd');

# Build a filename for the first news story for the given date
$html_file = $date.'0001.html';

# Open a file handle to the URL
$input = fopen ("$base_URL/$html_file", 'r');

# recode_file doesn't like file handles that point to remote files
# Circumvent this by using a temp file to store the URLs content
$tmp = tmpfile ();

# Write all data from the URL to the temp file
while (! feof ($input)) {
   # Read in a kilobyte of data from the URL
   $data = fgets ($input, 1024);

   # Convert any Windows-style line ends (\r\n) to Unix-style line ends (\n)
   # We do this because recode does not like it when we mix line ends within a file
   $data = str_replace ("\r\n", "\n", $data);

   # write the data to the temp file
   fwrite ($tmp, $data);
}

fclose ($input);    

# Return to the start of the temp file before reading data from it
rewind ($tmp);

# Open a handle to a new local file
$output = fopen ("./example.com/news_stories/$html_file", 'w');

# Convert from ASCII to flat (ASCII without diacritics or underlines)
# Suppress possible errors with a single @ symbol
@ recode_file ('us..flat', $tmp, $output);

# Close the temp file
# Note: When a file opened by tmpfile() is closed, it's automatically deleted
fclose ($tmp);

fclose ($output);
?>



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.