<?php
/*********************************
 * Pošalji header.				 *
 *********************************/
header("Content-Type: text/html; charset=UTF-8");
/****************************************************************************************************
 * @Autor: aleksandar@vavok.net																		*
 * @Funkcije: 5																						*
 * @Opis: Ova klasa vrši prebacivanje teksta iz latinice u ćirilicu (može i obrnuto)				*
 * @Recoded: Jezdimir Lončar ( jezdonet@gmail.com )													*
 ****************************************************************************************************
 * Ukoliko napišete neku modifikaciju za ovu skriptu, pošaljite je na email aleksandar@vavok.net.	*
 * TEMA NA ES: http://www.elitesecurity.org/t338451-Latin-to-Cyr-PHP-converter						*
 ****************************************************************************************************/
 
/****************************************************************************************************
 * @Autor: aleksandar@vavok.net																		*
 * @Funkcija: lat_to_cyr_skiptags																	*
 * @Opis: Ova funkcija vrši konvertovanje iz latinice u ćirilicu									*
 ****************************************************************************************************/
function lat_to_cyr_skiptags($buffer){
    $poz_current = 0;
    $poz_end = strlen($buffer)-1;
    $result = "";

    while ($poz_current < $poz_end) {
        $t_poz_start = stripos($buffer, "<", $poz_current);
        if ($t_poz_start === false) {
            $buffer_part_2strip = substr($buffer, $poz_current);
            $temp = lat_and_cyr($buffer_part_2strip);
            $result .= $temp;
            $poz_current = $poz_end;
        } else {
            $buffer_part_2strip = substr($buffer, $poz_current, $t_poz_start - $poz_current);
            $temp = lat_and_cyr($buffer_part_2strip);
            $result .= $temp;
            $t_poz_end = stripos($buffer, ">", $t_poz_start);
            $temp = substr($buffer, $t_poz_start, $t_poz_end - $t_poz_start);
            $result .= $temp;
            $poz_current = $t_poz_end;
        } 
    }
	$result = skip($result);
    return $result;
}
/****************************************************************************************************
 * @Autor: Jezdimir Lončar ( jezdonet@gmail.com )													*
 * @Funkcija: skipspecial																			*
 * @Opis: Funkcija sprečava konvertovanje određenih znaka u ćirilicu.								*
 *		  Više znaka možete dodati ako znate osnove PHP-a ;)										*
 ****************************************************************************************************/
function skipspecial($string) {	
	$search[0] = lat_and_cyr("&quot;");
	$replace[0] = "&quot;";
	$search[1] = lat_and_cyr("&amp;");
	$replace[1] = "&amp;";	
	$search[2] = lat_and_cyr("&lt;");
	$replace[2] = "&lt;";
	$search[3] = lat_and_cyr("&gt;");
	$replace[3] = "&gt;";	
	$search[4] = lat_and_cyr("&euro;");
	$replace[4] = "&euro;";
	$search[5] = lat_and_cyr("&bull;");
	$replace[5] = "&bull;";
	foreach($search as $s=>$r) {
	 $string = str_replace($r,$replace[$s],$string);
	}
	return $string;
}
/****************************************************************************************************
 * @Autor: Jezdimir Lončar ( jezdonet@gmail.com )													*
 * @Funkcija: skipspecial																			*
 * @Opis: Funkcija sprečava konvertovanje određenih riječi u ćirilicu.								*
 *		  Više riječi možete dodati ako znate osnove PHP-a ;)										*
 * @NAPOMENA: Case sensitive.																		*
 ****************************************************************************************************/
function skip($string) {
	$search[0] = lat_and_cyr("Microsoft");
	$replace[0] = "Microsoft";
	$search[1] = lat_and_cyr("microsoft");
	$replace[1] = "microsoft";
	
	$search[2] = lat_and_cyr("Browser");
	$replace[2] = "Browser";
	$search[3] = lat_and_cyr("browser");
	$replace[3] = "browser";
	
	$search[4] = lat_and_cyr("Email");
	$replace[4] = "Email";
	$search[5] = lat_and_cyr("email");
	$replace[5] = "email";
	foreach($search as $s=>$r) {
	 $string = str_replace($r,$replace[$s],$string);
	}
	return skipspecial($string);
}

/****************************************************************************************************
 * @Autor: Jezdimir Lončar ( jezdonet@gmail.com )													*
 * @Funkcija: cyr_to_lat																			*
 * @Opis: Vrši konvertovanje ćirilice u latinicu.													*
 ****************************************************************************************************/
function cyr_to_lat($str){
    $latin = array("dj", "lj", "nj", "dž", "dz", "a", "b", "v", "g", "d", "e", "ž", "z", "i", "j", "k", "l", "m", "n", "o", "p", "r", "s", "t", "ć", "u", "f", "h", "c", "č", "š", "Dj", "DJ", "Lj", "LJ", "Nj", "NJ", "DŽ", "Dž", "A", "B", "V", "G", "D", "E", "Ž", "Z", "I", "J", "K", "L", "M", "N", "O", "P", "R", "S", "T", "Ć", "U", "F", "H", "C", "Č", "Š");
    $cyrillic = array("ђ", "љ", "њ", "џ", "џ", "а", "б", "в", "г", "д", "е", "ж", "з", "и", "ј", "к", "л", "м", "н", "о", "п", "р", "с", "т", "ћ", "у", "ф", "х", "ц", "ч", "ш", "Ђ", "Ђ", "Љ", "Љ", "Њ", "Њ", "Џ", "Џ", "А", "Б", "В", "Г", "Д", "Е", "Ж", "З", "I", "Ј", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "Ћ", "У", "Ф", "Х", "Ц", "Ч", "Ш");
    $str = str_replace($cyrillic, $latin, $str);
    return $str;
}
/****************************************************************************************************
 * @Autor: aleksandar@vavok.net																		*
 * @Funkcija: lat_and_cyr																			*
 * @Opis: Vrši konvertovanje latinice u ćirilicu, ali ne pravi izuzetak kod tagova.					*
 ****************************************************************************************************/
function lat_and_cyr($str)
{ // Latinica u ćirilicu
    $latin = array("dj", "lj", "nj", "dž", "dz", "a", "b", "v", "g", "d", "e", "ž", "z", "i", "j", "k", "l", "m", "n", "o", "p", "r", "s", "t", "ć", "u", "f", "h", "c", "č", "š", "Dj", "DJ", "Lj", "LJ", "Nj", "NJ", "DŽ", "Dž", "A", "B", "V", "G", "D", "E", "Ž", "Z", "I", "J", "K", "L", "M", "N", "O", "P", "R", "S", "T", "Ć", "U", "F", "H", "C", "Č", "Š");
    $cyrillic = array("ђ", "љ", "њ", "џ", "џ", "а", "б", "в", "г", "д", "е", "ж", "з", "и", "ј", "к", "л", "м", "н", "о", "п", "р", "с", "т", "ћ", "у", "ф", "х", "ц", "ч", "ш", "Ђ", "Ђ", "Љ", "Љ", "Њ", "Њ", "Џ", "Џ", "А", "Б", "В", "Г", "Д", "Е", "Ж", "З", "I", "Ј", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "Ћ", "У", "Ф", "Х", "Ц", "Ч", "Ш");
    $str = str_replace($latin, $cyrillic, $str);
    return $str;
}

// Primjer
echo lat_to_cyr_skiptags('Sada je ćirilica :- ) microsoft Browser email &bull;');

?>