Funkce na konvertovani HTML na XHTML
Hlavni funkce [html_to_xhtml.php]
<?
function html_to_xhtml($text){
$text=quote_attrs($text); //uvozovky
$text=preg_replace_callback("|(<[^>]*>)|","small",$text); //small case
$text=preg_replace("#<(br|input|hr|img|meta|link)([^<>]*)(?<!/)>#", "<$1$2 />", $text);
$text=preg_replace("#<(input.*)(checked)([^>]*)>#","<$1 checked=\"checked\" $3>",$text); //checked v input
$text=preg_replace("#(<img\s+(\w*(.[^t]|[^l].)=\"[^\"]*\"\s*)*)/?>#","$1 alt=\"neco\" />",$text);
$text=preg_replace("#<(.*[^=])='(.*)'([^>]*)>#","<$1=\"$2\">",$text); //apostrof vs. uvozovka
$text=preg_replace("|<!doctype([^>]+)>|","<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",$text); //doctype
return $text;
}
?>
Funkce pro zmenseni vseho mimo uvozovky [small.php]
<?
function small($matches)
{
$matches[1]=$matches[1];
$a=explode("\"",$matches[1]);
for ($i=0;$i<sizeof($a);$i+=2){
//$a[$i]=StrToLower($a[$i]);
$a[$i]=StrToLower($a[$i]);
}
$text=implode($a,'"');
return ($text);
}
?>
Funkce pridavajici uvozovky [quote_attr.php]
<?
function quote_attr ($var)
{
$string=preg_replace('/= *([^"\'\s>]+)(\s|>)/','="\\1"\\2',$var[0]);
return $string;
}
?>
Callback vybirajici oblast mezi < a > [quote_attrs.php]
<?
function quote_attrs ($string)
{
$string=preg_replace_callback('/<([^>]+)>/', 'quote_attr', $string);
return $string;
}
?>
Funkce pro nacteni obsahu souboru [nacti_soubor.php]
<?
//echo "funkce nacti soubor byla docasne vyrazena";
function nacti_soubor($path) {
$soubor=explode("/",$path);
$path=$soubor[sizeof($soubor)-1];
$in = fopen($path, "r");
while ($line=fgets($in,4096))
{
$in_string.=$line;
}
fclose($in);
return $in_string;
}
?>
Pouziti [test.php]
http://koudis.webport.cz/skripty/html_to_xhtml/test.php?zdroj=test.html
<div class="h"><h2> Funkce na konvertovani HTML na XHTML </h2></div>
<?if (!isset($text)){?>
<form action="index.php?page=html_to_xhtml" method="post">
<br />
<center><input type="submit" value="Odeslat" /></center>
<center>HTML</center>
<center>
<textarea name="text" style="width: 95%" cols="40" rows="10"></textarea><br />
</center>
</form>
<?
}
//nacteni funkci
include('html_to_xhtml.php');
include('small.php');
include('quote_attr.php');
include('quote_attrs.php');
include('nacti_soubor.php');
//konec funkci
if (isset($text)){
echo "<center>XHTML</center>";
echo "<div class=\"urs\"><div class=\"urs-pre\"><pre>";
echo htmlspecialchars(StripSlashes(html_to_xhtml($text)));
echo "</pre></div><br>";
echo "<center>HTML</center>";
echo "<div class=\"urs-pre\"><pre>";
echo htmlspecialchars(StripSlashes($text));
echo "</pre></div></div><br>";
include("./lib/constants.php");
$meno="Koudis";
$mailik=$rmail;
$od="From: ".$meno." <".$mailik.">\r\n";
$predmet="HTML -> XHTML\n";
}
?>
<div class="h"><h2> Poznamka </h2></div>
<div class="text">
Skript byl zatim testovan jen na jednoduchych strankach. Podekovani patri lidem z klubu Regexp z nyx.cz, kteri mi znacne pomohli se vsemi funkcemi.
</div>
<div class="h"><h2> Zdrojove kody </h2></div>
<div class="text">
Zdrojove kody prevodniku jsou volne k dispozici. Nanestesti jsem nekde ztratil styl k tomuto dokumentu a jsem prilis linyhy napsat ho znovu :)<br />
<a href="http://koudis.net/body/html_to_xhtml/">http://koudis.net/body/html_to_xhtml/</a>
</div>
Styl [format.css]
Warning: fopen(format.css) [function.fopen]: failed to open stream: No such file or directory in /var/www/koudis.net/www/body/html_to_xhtml/nacti_soubor.php on line 6
Warning: fgets(): supplied argument is not a valid stream resource in /var/www/koudis.net/www/body/html_to_xhtml/nacti_soubor.php on line 7
Warning: fclose(): supplied argument is not a valid stream resource in /var/www/koudis.net/www/body/html_to_xhtml/nacti_soubor.php on line 11
Testovany soubor [test.html]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<Meta nejaky meta tag>
<link na styl>
</head>
<body>
<script type='javascipt/text'>/*nejaky script*/</script>
<input type=text name="Test">
<input type=checkbox checked name="DEFAULTNI VYBER">
<A HREF="VELKY.jpg"><img src="VELKY_obrazek"/></A>
<img src="obr.jpg" alt="popisek here" />
<img src="obr.jpg" alt=133kB >
<br><hr><HR>
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td>TEST</td>
<td>TEST</td>
</tr>
<tr>
<td>TEST</td>
<td>TEST</td>
</tr>
</table>
TENHLE TEXT DOLE JE TAKY VLEKYM PISMEM
<br><BR>
</body>
</html>