I don’t use [FPHP]htmlspecialchars_decode[/FPHP]. I need to explain this hack.
I believe that in a ideal world we should only have three desktop apps: a browser, an ftp package, and a text editor. Everything else should be a web app. Since PCs are now servers, we can use the web apps locally which is what I’m doing with the one I’m testing now.
My web app (which does not yet have a name) creates an HTML5 document which prints a letter just like a word processor document using a template for the letterhead and a text document for the letter body. In fact, the browser acts like a word processor. It’s a simple script really:
<?php
$files = "/Users/username/Sites/path/to/letters/";
// this is the html header and the letterhead
$html = <<< AVILAAVILA
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>Edifico Avila</title>
<link rel="stylesheet" type="text/css" href="avila.css">
</head>
<body>
<div class="logo">
<span>edif.<span>A</span>vila</span><br>
Avenida Buenos Aires, Los Caobos, Caracas.
</div>
AVILAAVILA;
// this is the letter's body
$html .= file_get_contents("+avila.html");
$html .= "</body>\
</html>\
";
// create a ubique file name
$date = date ("m-d-y");
$filename = "{$files}avila-{$date}.html";
$append = "";
while (file_exists ($filename)) {
$append += 1;
$filename = $filename = "{$files}avila-{$date}-{$append}.html";
if ($append > 10) {
die ("too many files already");
}
}
// adjust some URLs for archiving
$html1 = str_replace ("avila.css", "../avila.css", $html);
$html1 = str_replace ('src="images/', 'src="../images/', $html1);
$html1 = str_replace ('<body>', '<body style="border:none;">', $html1);
// save the file
if (file_put_contents ($filename, $html1) === FALSE) {
die ("Cannot write to file ($filename)");
}
// show some feedback on the screen
echo "<div class='no-print'>";
echo "Success, $filename";
echo "</div>";
// display the letter
echo $html;
?>
The problem is that in Spanish we use diacritical marks: accents, tildes, cedillas, and umlauts (áéíóúñçü) plus opening exclamation and question mark (¡¿). The best way to deal with them is to use the html entity codes. Typing the html entity codes is pita so I want to use some code to translate them. But htmlentities also converts the open and close html tags “<>” which I need to render the html correctly. I also have the option of using a BBEdit Text Factory but I prefer a php solution. I have a function for the conversion which works on my laptop but gives funny (UTF-8?) codes on my LAMP server.
function c2html($text) {
$table = array(
'¡' => '¡',
'¢' => '¢',
'£' => '£',
'©' => '©',
'®' => '®',
'°' => '°',
'¿' => '¿',
'Á' => 'Á',
'Ç' => 'Ç',
'É' => 'É',
'Í' => 'Í',
'Ñ' => 'Ñ',
'Ó' => 'Ó',
'Ú' => 'Ú',
'Ü' => 'Ü',
'á' => 'á',
'ç' => 'ç',
'é' => 'é',
'í' => 'í',
'ñ' => 'ñ',
'ó' => 'ó',
'ú' => 'ú',
'ü' => 'ü',
'&' => '&',
'¥' => '¥');
return strtr($text, $table);
}
Thanks for listening!