Re: [PHP] Why PHP won

2009-02-24 Thread Michael A. Peters

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



Re: [PHP] Why PHP won

2009-02-24 Thread Per Jessen
Michael A. Peters wrote:

 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.

I don't use any of them, but I thought even IE6 was able to deal with
xml. 

 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.

From http://www.w3.org/TR/xhtml-media-types/

In general, 'application/xhtml+xml' should be used for XHTML Family
documents, and the use of 'text/html' should be limited to
HTML-compatible XHTML Family documents intended for delivery to user
agents that do not explicitly state in their HTTP Accept header that
they accept 'application/xhtml+xml' [HTTP]. 


/Per

-- 
Per Jessen, Zürich (2.7°C)


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



Re: [PHP] Why PHP won

2009-02-24 Thread Michael A. Peters

Per Jessen wrote:



I don't use any of them, but I thought even IE6 was able to deal with
xml. 


What happens is IE6 (and I believe IE7) asks the user what application 
they want to open the file with if it receives an xml+xhtml header.


IE does parse xhtml but only if sent with an incorrect html header.

IE8 is suppose to fix that, but it will probably take years before IE6/7 
is for all practical purposes out of circulation.


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



Re: [PHP] Why PHP won

2009-02-23 Thread Paul M Foster
On Mon, Feb 23, 2009 at 01:39:51PM -0800, Daevid Vincent wrote:

 http://startuplessonslearned.blogspot.com/2009/01/why-php-won.html
 

I *like* the way this guy thinks.

Paul

-- 
Paul M. Foster

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



Re: [PHP] Why PHP won

2009-02-23 Thread Michael A. Peters

Paul M Foster wrote:

On Mon, Feb 23, 2009 at 01:39:51PM -0800, Daevid Vincent wrote:


http://startuplessonslearned.blogspot.com/2009/01/why-php-won.html



I *like* the way this guy thinks.

Paul



It was a decent page.
Point #2 though - you can use mod_rewrite to do wonders with respect to 
url presentation, 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.


Avoiding mixing html with php really is better, and php does let you do 
that fairly easily.


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



Re: [PHP] Why PHP won

2009-02-23 Thread Per Jessen
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.



-- 
Per Jessen, Zürich (2.1°C)


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