note 51675 added to function.preg-match-all

From: Date: Thu, 07 Apr 2005 09:24:34 +0000
Subject: note 51675 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-87635@lists.php.net to get a copy of this message
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 &#xFF; Hex -> Hello &#255; Hex Second example converts Decimal code to Hexadecimal code. ex) Hi &#16; Dec -> Hi &#x0010; 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); ?> ---- Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+51675 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51675&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51675&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51675&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51675&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51675&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+51675&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#87635) next »