recode_fileDescriptionrecode_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 . CautionThis function may return FALSE in some situations where it successfully recodes the character set. See alsoTo recode a single string Other functions are available for building internationalized applications in PHP: see the GNU Gettext functions. ExampleExample 1119. Fetch a news story from a web page and strip all the accents out of the text
NoteA 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.
|