Codul de mai jos trebuie pus pe host intr-un fisier pentru a functiona.
1. facem un fisier.php pe host
2. deschidem in editorul preferat si adaugam codul
3. accesam site.com/fisier.php si punem textul
1. facem un fisier.php pe host
2. deschidem in editorul preferat si adaugam codul
3. accesam site.com/fisier.php si punem textul
Cod:
<h2>Text to UTF-8 or HTML Entities</h2><?php
if (isset($_POST['text'])) {
$retain = $_POST['text'];
if ($_POST['method'] != "UTF8") { ?>
<h3>Copy these codes directly from the page</h3><?php
if ($_POST['method'] == "JS_UC") { ?>
<p>JavaScript escaped Unicode codes can be inserted into strings in JavaScript code.</p><?php
} else { ?>
<p>You can paste HTML entities as-is into any HTML document.</p><?php
}
function uniord($c) {
$ud = 0;
if (ord($c{0}) >= 0 && ord($c{0}) <= 127) $ud = ord($c{0});
if (ord($c{0}) >= 192 && ord($c{0}) <= 223) $ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
if (ord($c{0}) >= 224 && ord($c{0}) <= 239) $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
if (ord($c{0}) >= 240 && ord($c{0}) <= 247) $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
if (ord($c{0}) >= 248 && ord($c{0}) <= 251) $ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
if (ord($c{0}) >= 252 && ord($c{0}) <= 253) $ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
if (ord($c{0}) >= 254 && ord($c{0}) <= 255) $ud = false; // error
return $ud;
}
function unichr($dec) {
if ($dec < 128) {
$utf = chr($dec);
} else if ($dec < 2048) {
$utf = chr(192 + (($dec - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
} else if ($dec < 65536) {
$utf = chr(224 + (($dec - ($dec % 4096)) / 4096));
$utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
} else if ($dec < 2097152) {
$utf = chr(240 + (($dec - ($dec % 262144)) / 262144));
$utf .= chr(128 + ((($dec % 262144) - ($dec % 4096)) / 4096));
$utf .= chr(128 + ((($dec % 4096) - ($dec % 64)) / 64));
$utf .= chr(128 + ($dec % 64));
} else return ""; // error
return $utf;
}
$char = "";
while (strlen($_POST['text']) > 0) {
preg_match("/^(.)(.*)$/us", $_POST['text'], $match);
$test = utf8_decode($match[1]);
if ($_POST['method'] == "JS_UC") {
$uo = uniord($match[1]);
$char .= ($uo >= 32 && $uo <= 126) ? $match[1] : sprintf("\\u%04s", dechex($uo));
} else {
if ($test != "?") {
$char .= htmlentities(htmlentities($test));
} else if (strlen($match[1]) > 1) {
$char .= "&#".(($_POST['method'] == "HTML_HEX") ? "x".dechex(uniord($match[1])) : uniord($match[1])).";";
} else $char .= htmlentities(htmlentities($match[1]));
} $_POST['text'] = $match[2];
}
$_POST['text'] = $char;
if ($_POST['method'] == "JS_UC")
$_POST['text'] = htmlspecialchars($_POST['text']);
} else { ?>
<h3>Do not copy the text below directly!</h3>
<p>Instead, view the source of this page and copy everything between the <pre> tags</p>
<!-- ########## COPY EVERYTHING BETWEEN THE <pre> TAGS ########## --><?php
$_POST['text'] = htmlspecialchars($_POST['text'], ENT_NOQUOTES);
} ?>
<pre><?php echo $_POST['text']; ?></pre><?php
} ?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
<fieldset>
<legend>Translate Text</legend>
<textarea rows="5" cols="70" name="text"><?php echo (isset($retain)) ? htmlspecialchars($retain, ENT_NOQUOTES) : "Paste text to be translated"; ?></textarea><br />
<label>
Select method:
<select size="1" name="method">
<option value="UTF8"<?php if (!isset($_POST['method']) || $_POST['method'] != "HTML") echo " selected=\"selected\""; ?>>UTF-8</option>
<option value="HTML"<?php if (isset($_POST['method']) && $_POST['method'] == "HTML") echo " selected=\"selected\""; ?>>HTML Entities</option>
<option value="HTML_HEX"<?php if (isset($_POST['method']) && $_POST['method'] == "HTML_HEX") echo " selected=\"selected\""; ?>>HTML Entities (Hexidecimal)</option>
<option value="JS_UC"<?php if (isset($_POST['method']) && $_POST['method'] == "JS_UC") echo " selected=\"selected\""; ?>>JavaScript escaped Unicode</option>
</select>
</label>
<input type="submit" value="Translate" /><?php
if (isset($retain)) { ?>
<a href="<?php echo $_SERVER['REQUEST_URI']; ?>">Clear</a><?php
} ?>
</fieldset>
</form>