Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed 

serve suitable mime type with php

An updated version of the keystone method. If you are wanting to use it for a doctype other than XHTML 1.1 then a little easy editing is needed. Simon Jessey (another Brit') sent me this updated version which differs to the original in that it works just fine with the W3C Validator.

<?php


/*
    This script determines the preferred MIME type of a user agent
    and then delivers either application/xhtml+xml or text/html.
    Copyright (c) 2003 - 2004 Keystone Websites and Simon Jessey
*/


$charset = "utf-8";
$mime = "text/html";

function fix_code($buffer) {
    return (str_replace(" />", ">", $buffer));
}

if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
        if(preg_match("/application\/xhtml\+xml;q=([01]|0\.\d{1,3}|1\.0)/i",$_SERVER["HTTP_ACCEPT"],$matches)) {
                $xhtml_q = $matches[1];
                if(preg_match("/text\/html;q=q=([01]|0\.\d{1,3}|1\.0)/i",$_SERVER["HTTP_ACCEPT"],$matches)) {
                        $html_q = $matches[1];
                        if((float)$xhtml_q >= (float)$html_q) {
                        $mime = "application/xhtml+xml";
                        }
                }
        } else {
                $mime = "application/xhtml+xml";
                }
}

if(stristr($_SERVER["HTTP_USER_AGENT"],"WDG_Validator") ||
   stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")) {
        $mime = "application/xhtml+xml";
}

if($mime == "application/xhtml+xml") {
        $doc_head = "<?xml version=\"1.0\" encoding=\"$charset\"?>\n";
        $doc_head = $doc_head."\"-//W3C//DTD XHTML 1.1//EN\"";
        $doc_head = $doc_head." \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n";
        $doc_head = $doc_head."\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n\n";
        $doc_head = $doc_head."  \"http://gmpg.org/xfn/1\">\n";
} else {
        ob_start("fix_code");
    $doc_head = "\"-//W3C//DTD HTML 4.01 Transitional//EN\"";
    $doc_head = $doc_head." \"http://www.w3.org/TR/html4/loose.dtd\">\n";
    $doc_head = $doc_head."\"en\">\n\n";
    $doc_head = $doc_head."  \"http://gmpg.org/xfn/1\">\n";
    $doc_head = $doc_head."    \"content-type\" content=\"$mime;charset=$charset\">\n";
}

header("Content-Type: $mime;charset=$charset");
header("Vary: Accept");

print $doc_head;

?>


Replace you doctype declaration, mime type in the head of your document or template with:
<?php include("pathtowhateveryouhavecalledit.php"); ?>


Many thanks to Simon Jessey for this version.

Measure the daily number of E-mail messages in a mailbox

This snippet written in bash with calls to perl from the command line measures the number of E-mail messages sent to a mailbox per calendrical day.

#!/bin/bash
grep -h '^Date:' * |
    perl -pe 's!^Date: !!' |
    perl -pe 's!^\w\w\w, !!' |
    perl -pe 's{\d{2}:\d{2}:\d{2}.*$}{}' |
    perl -pe 's!^\s+!!' |
    perl -pae '$_=sprintf("%.2d-%s-%s\n", @F)' |
    sort | uniq -c | sort -n


I used perl for some places where sed would have been more suitable because I find the sed regexp syntax confusing. :-)
« Newer Snippets
Older Snippets »
2 total  XML / RSS feed