note 51675 deleted from function.preg-match-all by colder
| From: | colder@php.net | Date: | Mon, 16 Apr 2007 16:56:16 +0000 |
| Subject: | note 51675 deleted from function.preg-match-all by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125458@lists.php.net to get a copy of this message | ||
Note Submitter: b2sing4u
----
Character Code Conversion Example.
You can use following example to convert character code in HTML file.
First example converts Hexadecimal code to Decimal code.
ex) Hello ÿ Hex -> Hello ÿ Hex
Second example converts Decimal code to Hexadecimal code.
ex) Hi  Dec -> Hi  Dec
<?php
$h2d_get = fopen("h2d_get.htm", 'r');
$h2d_out = fopen("h2d_out.htm", 'w');
for ($i = 1; $i <= 1000; $i++)
{
if (feof($h2d_get)) { break; }
$line = fgets($h2d_get, 409600);
$line = trim($line);
if ($line == "99999999") { break; }
$n = preg_match_all("/&#x([0-9a-fA-F]+?);/", $line, $match, PREG_PATTERN_ORDER);
for ($j = 0; $j < $n; $j++)
{
$find = $match[0][$j];
$code = hexdec($match[1][$j]);
$push = sprintf("&#%u;", $code);
$line = eregi_replace($find, $push, $line);
}
fwrite($h2d_out, $line);
fwrite($h2d_out, "\r\n");
}
fclose($h2d_get);
fclose($h2d_out);
?>
<?php
$d2h_get = fopen("d2h_get.htm", 'r');
$d2h_out = fopen("d2h_out.htm", 'w');
for ($i = 1; $i <= 1000; $i++)
{
if (feof($d2h_get)) { break; }
$line = fgets($d2h_get, 409600);
$line = trim($line);
if ($line == "99999999") { break; }
$n = preg_match_all("/&#(\d+?);/", $line, $match, PREG_PATTERN_ORDER);
for ($j = 0; $j < $n; $j++)
{
$find = $match[0][$j];
$code = $match[1][$j];
$push = sprintf("&#x%04X;", $code);
$line = eregi_replace($find, $push, $line);
}
fwrite($d2h_out, $line);
fwrite($d2h_out, "\r\n");
}
fclose($d2h_get);
fclose($d2h_out);
?>