Posted By


daemondevin on 09/27/19

Tagged


Statistics


Viewed 11129 times
Favorited by 0 user(s)

getDoctype.inc.php


/ Published in: PHP
Save to your folder(s)

With this snippet you will have the ability to display the `` while sending the correct headers with support for content type negotiation.

It also will correct itself for the W3C validator which does not send the correct Accept header for `XHTML` documents.

Does **NOT** send `XHTML 1.1` to browsers that wont accept `application/xhtml+xml` because the snippet will make sure the browser groks XML before sending anything.
Visit: labs/PHP/DOCTYPE.php#bug-fix for details and a link to the W3C XHTML for more


Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * gaetDoctype
  4.  *
  5.  * Copyright 2016 by demon.devin <[email protected]>
  6.  * Created on 10-07-2016
  7.  *
  8.  * getDoctype is free software; you can redistribute it and/or modify it under the
  9.  * terms of the GNU General Public License as published by the Free Software
  10.  * Foundation; either version 2 of the License, or (at your option) any later
  11.  * version.
  12.  *
  13.  * getDoctype is distributed in the hope that it will be useful, but WITHOUT ANY
  14.  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  15.  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License along with
  18.  * getDoctype; if not, write to the Free Software Foundation, Inc., 59 Temple
  19.  * Place, Suite 330, Boston, MA 02111-1307 USA
  20.  *
  21.  * @package getdoctype
  22.  */
  23. /**
  24.  * getDoctype
  25.  *
  26.  * @author daemon.devin
  27.  * @link PortableAppz.x10.mx/
  28.  * @copyright 2016
  29.  * @package getDoctype
  30.  * @version 1.0
  31.  *
  32.  * With this snippet you will have the ability to display the DOCTYPE while sending the
  33.  * correct headers with support for content type negotiation.
  34.  *
  35.  * It also will correct itself for the W3C validator which does not send the correct Accept
  36.  * header for XHTML documents.
  37.  *
  38.  * Does NOT send XHTML 1.1 to browsers that wont accept application/xhtml+xml because the
  39.  * snippet will make sure the browser groks XML before sending anything.
  40.  * visit: labs/PHP/DOCTYPE.php#bug-fix for details and a link to the W3C XHTML for more
  41.  *
  42.  *
  43.  * PROPERTIES
  44.  * $doc string required Defaults: XHTML
  45.  * Either HTML or XHTML
  46.  *
  47.  * $type string optional Defaults: Strict
  48.  * One of three chooses between Strict, Transitional, or Frameset
  49.  *
  50.  * $ver string optional Defaults: 1.1
  51.  * For XHTML: 1.0 or 1.1 | For HTML5: 5
  52.  *
  53.  * $charset string optional
  54.  *
  55.  * $lang string optional
  56.  *
  57.  * $s string optional Defaults: ' '
  58.  * Specify prefered indentation. Either '\t' (tab) or ' ' (whitespace) or any combination
  59.  *
  60.  * USAGE
  61.  * require_once('getDoctype.inc.php');
  62.  *
  63.  * EXAMPLE
  64.  * To specify html5 use the following:
  65.  * $doc='html'
  66.  * $ver='5'
  67.  *
  68.  * Output:
  69.  * <!DOCTYPE html>
  70.  * <html lang="en">
  71.  * <head>
  72.  * <meta charset="UTF-8">
  73.  *
  74.  * To specify XHTML using Strict with version 1.1 use the following:
  75.  * $doc='xhtml'
  76.  * $type='strict'
  77.  * $ver='1.1'
  78.  *
  79.  * NOTE: This sends the XML declaration before the DOCTYPE
  80.  * but this will put IE into quirks mode which we don't
  81.  * want so it omits it for IE
  82.  *
  83.  * Output:
  84.  * <?xml version="1.0" encoding="utf-8"?>
  85.  * <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  86.  * "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  87.  * <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  88.  *
  89.  */
  90.  
  91. $doc = ''; // html or xhtml
  92. $type = ''; // choose between strict, transitional, or frameset
  93. $ver = ''; // XHTML: 1.0 or 1.1 | HTML: 4.01 or 5
  94. $charset = ''; // Charset (i.e. UTF-8)
  95. $lang = ''; // Language (i.e. en)
  96. $s = ''; // Indentation. Either '\t' (tab) or ' ' (whitespace) or any combination.
  97.  
  98. $output = '';
  99.  
  100. $media = array('HTML' => 'text/html', 'XHTML' => 'application/xhtml+xml');
  101.  
  102. $doc = strtoupper($doc);
  103. $type = strtolower($type);
  104.  
  105. $avail = 'PUBLIC'; // or SYSTEM, but we're not going there yet
  106.  
  107. // begin FPI
  108. $ISO = '-'; // W3C is not ISO registered [or IETF for that matter]
  109. $OID = 'W3C'; // unique owner ID
  110. $PTC = 'DTD'; // the public text class
  111.  
  112. $PTD = '';
  113. $PCL = 'EN'; // as far as I know the PCL is always English
  114. $URI = 'http://www.w3.org/TR/'; // DTDs are all under the Technical Reports (TR) branch @ W3C
  115.  
  116. $doc_top = '<html'; // what comes after the DOCTYPE of course
  117. $meta = '<meta ';
  118.  
  119. if($doc == 'HTML') {
  120.  
  121. if($ver == '5') {
  122.  
  123. $top = 'html';
  124. $media_type = $media['HTML'];
  125. $meta .= 'charset="'.strtoupper($charset).'" />';
  126.  
  127. }
  128. else {
  129.  
  130. $top = $doc;
  131. $media_type = $media[$doc];
  132.  
  133. $PTD = $doc.' 4.01'; // we're only supporting HTML 4.01 here
  134.  
  135. switch ($type) {
  136.  
  137. case 'frameset':
  138.  
  139. $PTD .= ' '.ucfirst($type);
  140. $URI .= 'html4/frameset.dtd';
  141. break;
  142.  
  143. case 'transitional':
  144.  
  145. $PTD .= ' '.ucfirst($type);
  146. $URI .= 'html4/loose.dtd';
  147. break;
  148.  
  149. case 'strict':
  150. default:
  151.  
  152. $URI .= 'html4/strict.dtd';
  153. }
  154. $meta .= "http-equiv=\"Content-Type\" content=\"$media_type; charset=".strtoupper($charset)."\">";
  155. }
  156. $doc_top .= ' lang="'.$lang.'">';
  157.  
  158.  
  159. }
  160. else {
  161.  
  162. // must be xhtml then, but catch typos
  163.  
  164. //if($doc != 'XHTML')
  165. $doc = 'XHTML';
  166.  
  167. $top = 'html'; // remember XML is lowercase
  168. $doc_top .= ' xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$lang.'"';
  169.  
  170. // return the correct media type header for this document,
  171. // but we should probably make sure the browser groks XML!
  172.  
  173. // the W3C validator does not send the correct Accept header for this family of documents, sigh
  174. if(stristr($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator')) $media_type = $media['XHTML'];
  175. else $media_type = (stristr($_SERVER['HTTP_ACCEPT'], $media['XHTML']))?$media['XHTML']:$media['HTML'];
  176.  
  177. // do NOT send XHTML 1.1 to browsers that don't accept application/xhtml+xml
  178. // see: labs/PHP/DOCTYPE.php#bug-fix for details and a link to the W3C XHTML
  179. // NOTES on this topic
  180.  
  181. if($media_type == $media['HTML'] and $ver == '1.1') $ver = '1.0';
  182.  
  183. if($ver == '1.1') {
  184. $PTD = implode(' ', array($doc, $ver));
  185. $URI .= 'xhtml11/DTD/xhtml11.dtd';
  186. }
  187. else {
  188. $PTD = implode(' ', array(
  189. $doc,
  190. '1.0',
  191. ucfirst($type)));
  192. $URI .= 'xhtml1/DTD/xhtml1-'.$type.'.dtd';
  193.  
  194. // for backwards compatibilty
  195.  
  196. $doc_top .= ' lang="'.$lang.'"';
  197. }
  198.  
  199. $doc_top .= '>'; // close root XHTML tag
  200.  
  201. // send HTTP header
  202. header('Content-type: '.$media_type.'; charset='.$charset);
  203.  
  204. // send the XML declaration before the DOCTYPE, but this
  205. // will put IE into quirks mode which we don't want
  206. if(isset($_SERVER['HTTP_USER_AGENT']) && ((strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') == false) || strpos($_SERVER['HTTP_USER_AGENT'],
  207. 'Trident') == false)) {
  208. $output .= '<?xml version="1.0" encoding="'.$charset.'"?>'.PHP_EOL;
  209. }
  210.  
  211. $meta .= "http-equiv=\"Content-Type\" content=\"$media_type; charset=".strtoupper($charset)."\" />";
  212.  
  213. }
  214.  
  215. $FPI = implode('//', array(
  216. $ISO,
  217. $OID,
  218. $PTC.' '.$PTD,
  219. $PCL));
  220.  
  221. if($doc == 'HTML' && $ver == '5') {
  222. $output .= '<!DOCTYPE '.$top.'>'.PHP_EOL;
  223. $output .= $doc_top.PHP_EOL;
  224. $output .= "$s<head>".PHP_EOL;
  225. $output .= "$s$s$meta".PHP_EOL;
  226. }
  227. else {
  228. $output .= "<!DOCTYPE $top $avail \"$FPI\" ".PHP_EOL."$s\"$URI\">".PHP_EOL."$doc_top".PHP_EOL."$s<head>".PHP_EOL."$s$s".
  229. $meta.PHP_EOL;
  230. }
  231. return $output;

URL: http://portableappz.x10.mx/extras/getDoctype

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.