Tutorial Script convert text in UTF-8

AlexH

Merg pe strada catre Mine...
Membru personal
Administrative
Freelancer
SEO Expert
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

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 .= "&amp;#".(($_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 &lt;pre&gt; 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>
    &nbsp; &nbsp; <input type="submit" value="Translate" /><?php
    if (isset($retain)) { ?> 
      &nbsp; &nbsp; <a href="<?php echo $_SERVER['REQUEST_URI']; ?>">Clear</a><?php
    } ?> 
  </fieldset>
</form>
 
Se poate folosi si pri comand in putty.
1. mergeti in directorul unde sunt fisierele text, srt, sub pentru a f convertite
2. nano convertsub.sh
3. adaugai codul de mai jos
Cod:
for file in *.srt; do
    iconv --from-code=ISO-8859-1 --to-code=UTF-8 "$file" -o "${file%.srt}.fix.srt"
done
4. chmod 0777 convertsub.sh
5. rulam conversia
./convertsub.sh

Puteti schimba ISO-8859-1 in orice alt set de caractere care vreti voi.
Scriptul este de folos daca faceti conversie ffmpeg si primi eroarea cu UTF-8
 
O alta metoda este cu https://notepad-plus-plus.org/download/v7.3.3.html
1. deschide fisierul srt sau alt format in notepad plus
2. click pe codare din meniu de sus
3. alegem Converteste in UTF-8
4. salvam fisierul

Aceasta metoda mentine diacriticile.
Este posibil ca la unele fisiere dupa conversie sa apara textul cu patrate negri. Rezolvarea este sa dati click pe normal cum era inainte. Apoi gaseste si inlocuieste tot la literele speciale, gen cele cu diacritice. Dupa ce terminati faceti conversia iar si va merge bine.

FFmpeg vede bine acest format dupa si-l adauga pe video. Si cu scriptul de aici Script automat de adaugare subtitrare pe filme faceti un video de 5.2Gb in 540MB fara sa isi piarda din calitate.
Plus are optiunea de optimizare browser care permite derularea la video inainte si inapoi repede fara sa astepti descarcarea acestuia un buffer.

Trebuie sa recunosc ca mi-a luat 10 zile sa gasesc solutia asta care sa functioneze bine pentru ffmpeg.

Aici sunt doar 3 solutii, dar am testat mult mai multe.
 
Loading...
Back
Sus