PhpDig.net

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




GNU Recode Functions


This small group of functions provides a basic interface to the GNU recode utility. Recode is used to convert files from one character set to another. The utility is quite flexible, with the ability to convert among about 150 different character sets. Note that some character sets can't be converted into some other character sets, and that not all of the conversions are reversible. Be sure to test for expected cases before relying on this utility in a production situation.

For more information on the mechanics of recode, consult the recode documentation (available at http://www.delorie.com/gnu/docs/recode/).

Example 1118. A simple web interface to recode()

<?php
###########################################################################
# Note: Not all character sets can be converted from one to another.      #
# The key to successful use of recode lies in reading the documentation   #
# that comes with recode!                                                 #
###########################################################################

# Make a system call to recode to find out what character sets it understands
$char_sets = trim (`recode --list`);

# Put the aliases for character sets onto their own line
$char_sets = ereg_replace (' ', "\n", $char_sets);

# Convert the lines into an array
$char_sets = explode ("\n", $char_sets);

# Sort the array
natcasesort ($char_sets);

# Convert the char set lines into select tag options
$char_sets = '<option>' . implode ("</option>\n\t<option>", $char_sets) . "</option>\n";

# If we know what character sets to recode between and we have text to recode
if ($before and $after and $text) {
   $text = recode ("$before..$after", $text);
} else {
   # Set up default values for the select boxes
   $before = $after = '-- Select a character set --';
}
?>
<pre>
<form>
Character set to convert from:
<select name="before">
    <option><?php print $before ?></option>
    <?php print $char_sets ?>
</select>

Character set to convert to:
<select name="after">
    <option><?php print $after ?></option>
    <?php print $char_sets ?>
</select>

Text to convert:
<textarea name="text"><?php print htmlentities (stripslashes ($text)) ?></textarea>

<input type="submit" value="Convert" />

</form>
</pre>

Table of Contents

recode_file
recode_string



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.