PHP Multi byte mode
Multi-byte UNICODE PHP.
Pokud pracujete s diakritikou budou se Vám hodit funkce, které začínají mb_ tedy multi-byte funkce.Abyste nemuseli do každé funkce udávat kódování viz příklad
$mbLen = mb_strlen('žluťoučký','utf-8');
přidávejte na začátek kódu internal encoding
mb_internal_encoding('utf-8');
$mbLen = mb_strlen('žluťoučký');
Příklad si vyzkoušejte a ověřte hodnoty $mbLen77LW NO topic_id
AD
Další témata ....(Topics)
List of usefull JavaSript functions and constans
var 1_radian_to_degree = 180 / Math.PI; // 57,295779513082320876798154814105 degree
var 1_degree_to_radian = Math.PI / 180; // 0,01745329251994329576923690768489 radian
var 360_degree = 2 * Math.PI * 1_radian_to_degree;
parseInt("10.33") // returns 10 string to integer
parseFloat("10.33") // returns 10.33 string to float
// change sign
var a = -1;
a = a * -1; // +1
// random sign
a *= Math.floor(Math.random()*2) == 1 ? 1 : -1;
var who_is = 58;
var type = (typeof who_is); // number
Math.E // natural logarithms, e, approximately 2.718281828459045
Math.LN10 // natural logarithm of 10, approximately 2.302585092994046
Math.LN2 // natural logarithm of 2, approximately 0.6931471805599453
Math.PI // approximately 3.141592653589793
Math.SQRT1_2 // square root of 1/2 which is approximately 0.7071067811865476
Math.SQRT2 // square root of 2, approximately 1.4142135623730951
Math.pow(4, 2) = 16 // 4 x 4 ... base 4 to the exponent 2 power
Math.abs(-5) absolute value of a number // return 5
Math.tan(degrees * Math.PI / 180) // get tangent from degrees
Math.tan(degree * Math.PI / 180)*adjacent = opposite
opposite / Math.tan(degree * Math.PI / 180) = adjacent
Math.acos(adjacent / hypotenuse) // angle of a right-angle triangle in radians
Math.atan(opposite / adjacent) // angle of a right-angle triangle in radians
var adjacent = Math.cos(radian) * hypotenuse; // leng of adjacent side
// find the point on a circle if you know the circle´s radius
var x = Math.cos(pointAngleInRadiansFromCentreOfCircle) * radius;
var y = Math.sin(pointAngleInRadiansFromCentreOfCircle) * radius;
Math.cbrt(64) // cube root of a number 4 (4 x 4 x 4)
Math.exp(xNumber) // returns e<sup>xNumber</sup> ... e == Math.E
Math.ceil() // always rounds a number up to the next largest integer
Math.ceil(1.001) // 2
Math.floor() // returns largest integer less than or equal to a given number
Math.floor(5.85) = 5
Math.floor(-5.15) = -6
const arrayA = [2, 3, 1];
Math.min(...arrayA) = 1;
Math.max(1, 3, 2) = 3;
Math.random() // returns 0.000.... - 0.99999999999
Math.random() * 3 // returns 0.000 - 2.99999999999
Math.round() // returns value of a number rounded to the nearest integer.
Math.round(0.9) // 1
Math.round(5.5) // 6
Math.round(5.05) // 5
Math.floor(Math.random() * 3) // returns 0, 1, 2
Date: 14.06.2020 - 08:38Ke konverzi diakritiky slouží v PHP mnoho funkcí, ale výsledky jsou různé.
Někdy je lepší si udělat vlastní funkci.
Níže uvedená funkce zamění v řetězci paznaky, za jejich čitelný ekvivalent:
Někdy je lepší si udělat vlastní funkci.
Níže uvedená funkce zamění v řetězci paznaky, za jejich čitelný ekvivalent:
/**
* konverze diakritiky - okhelp.cz
*/
function diakritika($vstup) {
$arMala = array('á'=> 'á', 'ÄŤ'=> 'č', 'ÄŹ'=> 'ď', 'Ă©'=> 'é', 'Ä›'=> 'ě',
'Ă'=> 'i', 'Ĺ'=> 'ň', 'Ăł'=> 'ó', 'Ĺ™'=> 'ř', 'š'=> 'š', 'ĹĄ'=> 'ť', 'Ăş'=> 'ú', 'ĹŻ'=> 'ů', 'Ă˝'=> 'ý', 'Ĺľ'=> 'ž' );
$arVelka = array('Ă'=> 'Á', 'Ä©'=> 'Č', 'Ĭ'=> 'Ď', 'É'=> 'É', 'ı'=> 'Ě',
'Ă«'=> 'Í', 'Ň'=> 'Ň', 'Ă"'=> 'Ó', 'Ĺ'=> 'Ř', 'Ĺ '=> 'Š', 'Ť'=> 'Ť', 'ñ'=> 'Ú', 'ĹŽ'=> 'Ů', 'ö'=> 'Ý', 'Ĺ˝'=> 'Ž' );
foreach ($arMala as $key=>$value) {
$vstup = str_replace($key, $value, $vstup);
}
foreach ($arVelka as $key=>$value) {
$vstup = str_replace($key, $value, $vstup);
}
return $vstup;
}
mb_internal_encoding("UTF-8");
$s = 'áčď';
$novy = diakritika($s); // $novy == ačď
/*******************
* převede kůň na kun
* máte možnosti buď si napíšete funkci sami něci jako diakritikaKlasicka($vstup)
* nebo použijete $word = iconv("utf-8", "us-ascii//TRANSLIT", $word);
*ale výsledek otestujte, ne vždy je u iconv uspokojivý
*/
$word = "kůň";
$word = iconv("utf-8", "us-ascii//TRANSLIT", $word);
function diakritikaKlasicka($vstup) {
$arMala = array('á'=> 'a', 'č'=> 'c', 'ď'=> 'd', 'é'=> 'e', 'ě'=> 'e',
'í'=> 'i', 'ň'=> 'n', 'ó'=> 'o', 'ř'=> 'r', 'š'=> 's', 'ť'=> 't', 'ú'=> 'u',
'ů'=> 'u', 'ý'=> 'y', 'ž'=> 'z' );
$arVelka = array('Á'=> 'A', 'Č'=> 'C', 'Ď'=> 'D', 'É'=> 'E', 'Ě'=> 'E',
'Í'=> 'I', 'Ň'=> 'N', 'Ó'=> 'O', 'Ř'=> 'R', 'Š'=> 'S', 'Ť'=> 'T',
'Ú'=> 'U', 'Ů'=> 'U', 'Ý'=> 'Y', 'Ž'=> 'Z' );
foreach ($arMala as $key=>$value) {
$vstup = str_replace($key, $value, $vstup);
}
foreach ($arVelka as $key=>$value) {
$vstup = str_replace($key, $value, $vstup);
}
return $vstup;
}
Pokud často pracujete v PHP se soubory, tak se Vám může hodit
tato funkce, která vrací obsah souboru, jako string, narozdíl od funkce
file(), která vrací obsah souboru, jako pole řádků.
tato funkce, která vrací obsah souboru, jako string, narozdíl od funkce
file(), která vrací obsah souboru, jako pole řádků.
/**
* function return file content as string
* $filename is path to file
*/
function getContents($filename) {
if (function_exists('file_get_contents')) {
return file_get_contents($filename);
} else {
$fp = fopen($filename, 'r');
if (!$fp) {
echo ('Could not read contents <br/>');
return false;
}
return fread($fp, filesize($filename)); // return string
}
}
/**
* function file return file content as an array of rows
*/
$rows = file($filename);
/**
* fopen() modes
*/
r Read
r+ Read and write, prepend
w Write, truncate
w+ Read and write, truncate
a Write, append
a+ Read and write, append
Example code
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.newsite.com/new-page.php");
header("Connection: close");
exit();
If using file name without .php as part of smoothly url - example:
$fileName = basename(__FILE__, '.php');
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.mysite.cz/auto-moto/".$fileName."/");
header("Connection: close");
exit();
Date: 23.07.2020 - 12:31
Příklad vychází z předpokladu, že těsně před konečným
tagem /BODY je kontejner tag s názvem < footer>< /footer>.
Mezeru < TAGNAME nutno odstranit v reálném kódu.
Pokud tento tag je nalezen, lze předpokládat, že uživatel
stránku již uvidí celou, protože většina prohlížečů si s
tím poradí a doplní chybějící uzavírací tagy BODY a HTML.
// < TAGNAME remove space after < !!!!!
< BODY onload="myFunction">
< sript>
function myFunction() {
var x = document.getElementsByTagName ("footer");
if(x.length === 0)
alert("Page not loaded correctly ... not find tag footer x" + x);
else
alert("Heureka! :) FOOTER is loaded!");
}
< /script>
Editace: 18 10 2016
Počet článků v kategorii: 77
Url:php-multi-byte-mode