Re: Why PHP won

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Per Jessen wrote:
Michael A. Peters wrote:

[anip]
and you can use DOMDocument to completely
construct the page before sending it to the browser - allowing you to
translate xhtml to html for browsers that don't properly support
xhtml+xml.

I suspect you meant "translate xml to html"?  I publish everything in
xhtml, no browser has had a problem with that yet.



IE 6 does not accept the xml+html mime type. I don't believe IE7 does either, I think IE 8 beta does but I'm not sure.

If you are sending xhtml with an html mime type you are breaking a standard, even if it seems to work. xhtml is suppose to be sent with the xml+xhtml mime type.

by translating from xhtml to html 4.01 you can send the text/html mime type to browsers that don't report they accept applicatoiion/xml+xhtml and send them standards compliant html.

By using DOMDocument it is really easy to do.

Standards compliance is important, and it isn't hard to achieve.

The following is the filter I use for browsers that don't accept xml+html:

function HTMLify($buffer) {
/* based on http://www.kilroyjames.co.uk/2008/09/xhtml-to-html-wordpress-plugin */
   $xhtml[] = '/<script([^<]*)\/>/';
   $html[]  = '<script\\1></script>';

   $xhtml[] = '/<div([^<]*)\/>/';
   $html[]  = '<div\\1></div>';

   $xhtml[] = '/<a([^<]*)\/>/';
   $html[]  = '<a\\1></a>';

   $xhtml[] = '/\/>/';
   $html[]  = '>';

   return preg_replace($xhtml, $html, $buffer);
   }

That's only part of the work, but it is the most important part.
Note that that doesn't translate all valid xhtml - it doesn't for example take into account legal whitespace that DOMDocument doesn't produce. If you want more robust translator, use an xslt filter, but this function is simple and doesn't require the various xslt parameters and libraries at php compile time.

When I create a new DOMDocument class instance -

$myxhtml = new DOMDocument("1.0","UTF-8");
$myxhtml->preserveWhiteSpace = false;
$myxhtml->formatOutput = true;
$xmlHtml = $myxhtml->createElement("html");
if ($usexml == 1) {
   $xmlHtml->setAttribute("xmlns","http://www.w3.org/1999/xhtml";);
   $xmlHtml->setAttribute("xml:lang","en");
   }

when the document is created - instead of printing the output I save it as a variable and then add the proper DTD:

function sendpage($page,$usexml) {
$xhtmldtd="\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\";>\n"; $htmldtd="<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\";>";

   if ($usexml == 0) {
$bar=preg_replace('/<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>/',$htmldtd,$page,1);
      $bar = HTMLify($bar);
      } else {
      $bar=preg_replace('/\n/',$xhtmldtd,$page,1);
      }
   sendxhtmlheader($usexml);
   print($bar);
   }

I add the DTD there with preg_replace because I haven't yet found a DOMDocument way to add it, which is how it should be done - someone told me the DTD is currently read only in DOMDocument and that in the future, I'll be able to add it when a new DOMDocument class is created, so for now, the preg_replace hack works, and I might keep it that for some time.

To determine whether or not to send xhtml - when the page is requested (before any of that other code is executed) the following code is run:

if (! isset($usexml)) {
   $usexml=1;
   }
if ($usexml == 1) {
   if (isset( $_SERVER['HTTP_ACCEPT'] )) {
      if(! strpos( $_SERVER['HTTP_ACCEPT'], "application/xhtml+xml" ) ) {
         $usexml=0;
         }
      } else {
      $usexml=0;
      }
   }

The sendxhtmlheader function:

function sendxhtmlheader($usexml) {
   if ($usexml == 1) {
      header("Content-Type: application/xhtml+xml; charset=utf-8");
      } else {
      header("Content-type: text/html; charset=utf-8");
      }
   }

Anyway - all that is in a file I call "xhtml.inc" that I include in all my pages, then to build the document, I use the $myxhtml object that include creates. To send the document to the browser -

$foo=$myxhtml->saveXML();
sendpage($foo,$usexml);

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux