FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


character -> HTML entities converter for Hungarian letter





teanarancs
Good day !

Here is my converter written in PHP5. I use this as console script to convert my html documents - written in ANSI CP 1250 format (!). (Not in UTF-8 !!!)
The converter changes Hungarian characters into HTML entities.

I hope I do not corrupt my codes during the upload, anyway, if you have trouble with it, you can send me a message.

karent.php

Code:


<?php

// ANSI CP 1250 , CR+LF

//karent.php

/*

copyright(c)2015-2016 Héder Gábor

You can freely mention or copy or transfer or store or publish or modify this script.
You can freely use for any development this script.

When you modify this script or use large part of this script for development,
you have to mention the author's name of this script and the name of this script.
For best example :
'script karent.php , copyright(c)2015 Héder Gábor'

This script converts source/text files.
Aimfully, the text of the original file contents certain Hungarian letters, such as Ű,Ő,á,é,í etc.
After the convertion the new source file contents their HTML entities in the texts.

//------------------------------------------------------

// usage :

php karent.php original_source.any_extention new_extention

// for example :

php karent.php mypage.htm phtml

// It shall produce a converted version
//of 'mypage' with the given new extention :

mypage.phtml

************************
VERY IMPORTANT !
Existing file with html extention which has the same name (without extention)
as the original source without extention, shall be overwritten promptly,
without any notification !

*/


//variables
$file = "" ;
$newfile = "" ;
$filename = "" ;
$filenametext = "" ;
$newfilename = "" ;
$extention = "" ;
$newextention = "" ;
$szoveg = "" ;
$filesize = 0 ;
$szoveghossz = 0 ;
$filenamehossz = 0 ;
$extentionhossz = 0 ;
$fileopened = false ;
$hiba = false ;

//-------------------------------------------------------------------------------------

//arguments

print "$"."argv[0] : ".$argv[0]."\r\n" ;
print "$"."argv[1] : ".$argv[1]."\r\n" ;
print "$"."argv[2] : ".$argv[2]."\r\n" ;

if ( 1 < $argc and $argc < 4 )
{

$filename = $argv[1] ;
$filenametext = rtrim( $filename ) ;
$filenamehossz = strlen( $filenametext ) ;

if ( $argc == 3 )
{

if ( $argv[2][0] == "." )
{
$newextention = $argv[2] ;
}
else
{
$newextention = ".".$argv[2] ;
}

}
else
{
$newextention = ".html" ;
}

}
else
{
echo "ERROR : Wrong number of arguments ! " ;
$hiba = true ;
}

if (file_exists( $filename ) == true and $hiba == false )
{
$filenametext = strtolower( $filenametext ) ;
}
else
{
if ( $hiba == false ) echo "ERROR : No file found ! " ;
$hiba = true ;
}

if ( $filenamehossz != 0 and $hiba == false )
{
$extention = strrchr( $filenametext , "." ) ;
$extentionhossz = strlen($extention) ;
}
else
{
if ( $hiba == false ) echo "ERROR : Wrong filename ! " ;
$hiba = true ;
}

if ( $extention == ".html" )
{
if ( $hiba == false )
echo "Original file name with 'html' extention ! The script will EXIT. " ;
$hiba = true ;
}

$filesize = filesize( $filename ) ;

if ( $filesize == 0 )
{
if ( $hiba == false ) echo "ERROR : The file size is 0 ! " ;
$hiba = true ;
}

if ( $filesize != false and $hiba == false )
{
$file = fopen( $filename , "r") ;
}
else
{
if ( $hiba == false ) echo "ERROR : filesize() error ! " ;
$hiba = true ;
}

if ( $file == false )
{
if ( $hiba == false )
echo "ERROR : The script could not open the original file ! " ;
$hiba = true ;
}

$newfilename =
substr( $filenametext , 0 , $filenamehossz - $extentionhossz ).$newextention ;

if ( $hiba == false )
{

print "--> ".$newfilename."\r\n" ;
$szoveg = file_get_contents( $filename ) ;

if ( $szoveg != false )
{
$szoveghossz = strlen( $szoveg ) ;
}
else
{
if ( $hiba == false )
echo "ERROR : file_get_contents() error ! " ;
$hiba = true ;      
}

}

if ( $szoveghossz != 0 and $hiba == false )
{
require "modules/functions.php" ;
$szoveg = betucsere($szoveg) ;
}
else
{
if ( $hiba == false ) echo "ERROR : Empty file ! " ;
$hiba = true ;
}

if ( $file != false ) fclose( $file ) ;

//-------------------------------------------------------------------------------------

//newfile :

if ( $hiba == false )
{ // hiba

$newfile = fopen( $newfilename , "w") ;

if ( $newfile != false )
{

if ( fwrite( $newfile , $szoveg ) != false )
{
print "READY ! " ;
}
else
{
if ( $hiba == false )
echo "ERROR : The script could not write into the new file ! " ;
$hiba = true ;
}

fclose( $newfile ) ;

}
else
{
if ( $hiba == false )
echo "ERROR -- The script could not open or create the new file ! " ;
$hiba = true ;
}

} // hiba

print "\r\n" ;

?>



modules/functions.php :

Code:


<?php

// ANSI CP 1250 , CR+LF

// copyright(c)2010-2015 He`der Ga`bor



//------------------------------------------------------------

//Creates a string from the value of the predefined constant DATE_ATOM

function datumido()
{ // begin function datumido

// declare variable
$datumido = "" ;

$datumido = date(DATE_ATOM);
$datumido = substr_replace($datumido, " ", 10, 1) ;
$datumido = substr_replace($datumido, "", 19, 6) ;

return $datumido ;

} // end function datumido

//------------------------------------------------------------

/*
This is a short function for checking texts.
For example for the "POST" method.
If there is no text from the text field
(only "space" or "spaces" or nothing were written in it)
the value of the function will be "false".
*/

function textproba($textszoveg)
{ // begin function textproba

// declare variables
$logikai = "false" ; $n = 0 ; $karakter = "" ;

$n = strlen($textszoveg) ;

if ($n == 0)
{
$logikai = "false" ;
}
else if (empty($textszoveg))
{
$logikai = "false" ;
}
else
{
while ( ($logikai == "false") && ($n > 0 ) )
{
$karakter = substr($textszoveg,$n-1,1) ;
if  (  !( ($karakter == "") || ($karakter == " ") )  )  $logikai = "true" ;
$n = $n - 1 ;   
}
}

return $logikai ;

} // end function textproba

//------------------------------------------------------------

/*
This is a shorter version of function textproba() for checking texts
Without function empty()
*/

function txtproba($textszoveg)
{ // begin function txtproba

// declare variables
$logikai = "false" ; $n = 0 ; $karakter = "" ;

$n = strlen($textszoveg) ;

if ($n == 0)
{
$logikai = "false" ;
}
else
{
while ( ($logikai == "false") && ($n > 0 ) )
{
$karakter = substr($textszoveg,$n-1,1) ;
if  (  !( ($karakter == "") || ($karakter == " ") )  )  $logikai = "true" ;
$n = $n - 1 ;   
}
}

return $logikai ;

} // end function txtproba

//------------------------------------------------------------

//SZOURLEV function !
//Function : Reads texts from text file


function szovegolvaso($filename)
{ // begin function szovegolvaso

// declare variables
$indexfile = "" ; $szoveg = "" ;
global $logszoveg ;
global $hiba ;

if ( file_exists("$filename") )
{ // begin if file_exists

$indexfile = fopen("$filename", "r") ;

if ( $indexfile != false )
{ // begin if fopen

$szoveg = file_get_contents("$filename") ;

if ( $szoveg == false )
{ // begin if not file_get_contents

echo "Error : Could not read any text of $filename into a string variable ! " ;

// LOG { **********************
$logszoveg = "$logszoveg"."\r\n"."Error : Could not read any text of $filename into a string variable ! " ;
// LOG } **********************

$hiba = "true" ;

} // end if not file_get_contents

fclose($indexfile);

} // end if fopen
else
{ // begin else fopen

echo "Error : Could not open the text file $filename ! " ;

// LOG { **********************
$logszoveg = "$logszoveg"."\r\n"."Error : Could not open the text file $filename ! " ;
// LOG } **********************

$hiba = "true" ;
} // end else fopen

} // end if file_exists
else
{ // begin else file_exists

echo "Error : The file $filename does not exist ! " ;

// LOG { **********************
$logszoveg = "$logszoveg"."\r\n"."Error : The file $filename does not exist ! " ;
// LOG } **********************

$hiba = "true" ;
} // end else file_exists


return $szoveg ;

} // end function szovegolvaso

//------------------------------------------------------------------------------------

//This function replaces some kind of Hungarian characters with Entity Codes

function betucsere($textszoveg)
{ // begin function betucsere

// declare variables
$szoveghossz = 0 ; $n = 0 ;
$szoveg = "" ;  $karakter = "" ;

$szoveghossz = strlen($textszoveg) ;

if ( $szoveghossz != 0 )
{ // begin if not szoveghossz

$n = $szoveghossz ;

while ($n > 0 )
{ // begin while

$karakter = "" ;
$karakter = substr($textszoveg,$n-1,1) ;

switch ($karakter)
{ // begin switch

case chr(138) : $karakter = "Š" ; break ;
case chr(142) : $karakter = "Ž" ; break ;
case chr(154) : $karakter = "š" ; break ;
case chr(158) : $karakter = "ž" ; break ;
case chr(169) : $karakter = "©" ; break ;
case chr(174) : $karakter = "®" ; break ;
case chr(193) : $karakter = "Á" ; break ;
case chr(194) : $karakter = "Â" ; break ;
case chr(196) : $karakter = "Ä" ; break ;
case chr(199) : $karakter = "Ç" ; break ;
case chr(201) : $karakter = "É" ; break ;
case chr(203) : $karakter = "Ë" ; break ;
case chr(205) : $karakter = "Í" ; break ;
case chr(206) : $karakter = "Î" ; break ;
case chr(208) : $karakter = "Ð" ; break ;
case chr(211) : $karakter = "Ó" ; break ;
case chr(212) : $karakter = "Ô" ; break ;
case chr(214) : $karakter = "Ö" ; break ;
case chr(218) : $karakter = "Ú" ; break ;
case chr(220) : $karakter = "Ü" ; break ;
case chr(221) : $karakter = "Ý" ; break ;
case chr(223) : $karakter = "ß" ; break ;
case chr(225) : $karakter = "á" ; break ;
case chr(226) : $karakter = "â" ; break ;
case chr(228) : $karakter = "ä" ; break ;
case chr(231) : $karakter = "ç" ; break ;
case chr(233) : $karakter = "é" ; break ;
case chr(235) : $karakter = "ë" ; break ;
case chr(237) : $karakter = "í" ; break ;
case chr(238) : $karakter = "î" ; break ;
case chr(240) : $karakter = "ð" ; break ;
case chr(243) : $karakter = "ó" ; break ;
case chr(244) : $karakter = "ô" ; break ;
case chr(246) : $karakter = "ö" ; break ;
case chr(250) : $karakter = "ú" ; break ;
case chr(252) : $karakter = "ü" ; break ;
case chr(253) : $karakter = "ý" ; break ;

case chr(213) : $karakter = "Ő" ; break ;
case chr(219) : $karakter = "Ű" ; break ;
case chr(229) : $karakter = "Í" ; break ;
case chr(245) : $karakter = "ő" ; break ;
case chr(251) : $karakter = "ű" ; break ;
case chr(461) : $karakter = "Í" ; break ;
case chr(467) : $karakter = "Ó" ; break ;
case chr(469) : $karakter = "Ő" ; break ;
case chr(470) : $karakter = "Ö" ; break ;
case chr(474) : $karakter = "Ú" ; break ;
case chr(475) : $karakter = "Ű" ; break ;
case chr(476) : $karakter = "Ü" ; break ;
case chr(479) : $karakter = "ß" ; break ;
case chr(481) : $karakter = "á" ; break ;

case chr(484) : $karakter = "ä" ; break ;
case chr(485) : $karakter = "Í" ; break ;
case chr(489) : $karakter = "é" ; break ;
case chr(491) : $karakter = "ë" ; break ;
case chr(493) : $karakter = "í" ; break ;
case chr(499) : $karakter = "ó" ; break ;

} // end switch

$szoveg = $karakter.$szoveg ;

$n = $n - 1 ;

} // end while

} // end if not szoveghossz

return $szoveg ;

} // end function betucsere

//------------------------------------------------------------------------------------


// character <--> number coder functions (these are used in a datatable creating process)


function szamkar($szam)
{ // begin function szamkar()

if ( (1 <= $szam)&&($szam <= 10) )
{
$kar = chr($szam+47) ;   
}
else if ( (11 <= $szam)&&($szam <= 36) )
{
$kar = chr($szam+54) ;   
}
else if ( (37 <= $szam)&&($szam <= 62) )
{
$kar = chr($szam+60) ;   
}
else if ($szam == 63)
{
$kar = "_" ;      
}
else
{
$kar = "?" ;      
}

return $kar ;

} // end function szamkar()

//--------------------------------

function karszam($karakter)
{ // begin function karszam()

if ( $karakter == "_" )
{
$a = 63 ;      
}
else
{ // begin else karakter

$a = ord($karakter) ;

if ( (48 <= $a)&&($a <= 57) )
{
$a = $a - 47 ;
}
else if ( (65 <= $a)&&($a <= 90) )
{
$a = $a - 54 ;      
}
else if ( (97 <= $a)&&($a <= 122) )
{
$a = $a - 60 ;      
}
else
{
$a = 64 ;   
}

} // end else karakter

return $a ;

} // end function karszam()

//------------------------------------------------------------

//This is the encryption function.

function kodolo($szoveg,$tkulcs,$bomm)
{
$szoveg = mcrypt_encrypt(MCRYPT_BLOWFISH,$tkulcs,$szoveg,MCRYPT_MODE_CBC,$bomm) ;
$szoveg = base64_encode($szoveg) ;
return $szoveg ;
}

//------------------------------------------------------------

//This is the decryption function.

function dekodolo($szoveg,$tkulcs,$bomm)
{
$szoveg = base64_decode($szoveg) ;   
$szoveg = mcrypt_decrypt(MCRYPT_BLOWFISH,$tkulcs,$szoveg,MCRYPT_MODE_CBC,$bomm) ;
$szoveg = rtrim($szoveg) ;
return $szoveg ;
}

//------------------------------------------------------------

//This is a short function for $_POST method.

function posta($posta)
{ // begin function posta

//declare variable
$szoveg = "" ;

if (empty($_POST["$posta"]))
{
$szoveg = "" ;
}
else
{
$szoveg = $_POST["$posta"] ;
}

$szoveg = rtrim($szoveg) ;
return $szoveg ;

} // end function posta

//------------------------------------------------------------

//This is a short function for $_GET method.

function getposta($posta)
{ // begin function getposta

//declare variable
$szoveg = "" ;

if (empty($_GET["$posta"]))
{
$szoveg = "" ;
}
else
{
$szoveg = $_GET["$posta"] ;
}

$szoveg = rtrim($szoveg) ;
return $szoveg ;

} // end function getposta

//------------------------------------------------------------

//this is a short function for $_POST method for numbers

function szamposta($posta)
{ // begin function szamposta

if (empty($_POST["$posta"]))
{
$szam = 0 ;
}
else
{
$szam = $_POST["$posta"] ;
}

return $szam ;

} // end function szamposta

//------------------------------------------------------------

/*
creates a check code (ellenorzokod) randomly
requires function datumido()
*/

function ellenorzokod($n)
{ //begin function ellenorzokod

// declare variables
$szoveg = "" ; $szoveghossz = 0 ; $ellenorzokod = "" ;
$masodperc = "" ; $perc = "" ; $datumido = "" ;

// Gives value
$szoveg = "sA3g5hJaU3K2l1Urs3dK3TZfJ62F3dRT9fUj3Zu5bF3K4jK4z3R2EuR31RPkZ38zW5RTkzU3IoP5QuY3XCVi7l45k1l9jk8f7Aa6Qsw2E1VdMv0Lc2YXm53SaDl5RtZj5J4aKwy8Kj4G74DfSA3DT2zH168D43TZU51f3hlt3f5Ota6Eas5g9g8j4uFu53NKdF7Hf5S3KfgEhKF" ;

$datumido = datumido() ;
$masodperc = substr($datumido,17,2) ;
$perc = substr($datumido,14,2) ;

$szoveghossz = 200 ;

if  ($n > 8)
{
$n = $n - 4 ;
}
else
{
$n = 4 ;   
}

$ellenorzokod = $masodperc ;

while ($n > 0 )
{
$ellenorzokod = $ellenorzokod.substr($szoveg,rand(1,$szoveghossz),1) ;
$n = $n - 1 ;
}

$ellenorzokod = $ellenorzokod.$perc ;

return $ellenorzokod ;

} //end function ellenorzokod

//------------------------------------------------------------

/*
Creates a key randomly
(This function is a modified ellenorzokod() code.)
*/

function kodcsinalo($n)
{ //begin function kodcsinalo
   
// declare variables
$szoveg = "" ; $szoveghossz = 0 ; $amiga = "" ;

// gives value
$szoveg = "aX#(KH%FOK&=YVUuiIgC@/&=U7CXsd1TJ,%FJxc(B2G(#HH>z+fgh@/&=U7,%FJxcv9Z-*NBjklqTA08@Rq46!>5Nb6W3En958m4MUuiIO$+)IuoRRip3m%eertzG(#HtzG(#Ha7H698dK5rhtj7z4uBk2o3HO%K6Y9YerMJxC8gVhzUuiIO%FS5uSD4Fw!85/AYerMJxc(gCXsd1" ;

$szoveghossz = 200 ;

if  ($n < 8) $n = 8 ;

while ($n > 0 )
{
$amiga = $amiga.substr($szoveg,rand(1,$szoveghossz),1) ;
$n = $n - 1 ;
}

return $amiga ;

} //end function kodcsinalo

//------------------------------------------------------------

?>

Related topics
Adding <br /> to text area
[PHP] HTML entities, inputs, quotes
RSS Generator script
Getting Hebrew User Input...
Mysql 101 question
HTML Tutorial.
Store data format as user entry
html results page
Parsing feed tag urls
Parse feed tags/urls using rss2html
Apache
Wanting a PHP input security script: up to 1200frih$!
Editing Scripts Online
I need help with a very simple php code :(
Extract non-code from website
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.