My name is Edward Tanguay. I'm an American software and web developer living and working in Berlin, Germany.
PHP
One of the most popular, ubiquitous and useful scripting languages.
 
PHP TOOL created on Monday, December 08, 2008 permalink
Free PHP library to keep web app safe from malicious input, 2MB or so, easy to use (two lines).
 
PHP RADAR ITEM created on Monday, December 08, 2008 permalink
Tried it out, worked within 5 minutes, nice macros, try it.
 
FORAY INTO PHP created on Friday, January 09, 2009 permalink
Trying to get my twitter posts to show up on my website, SUCCEED
  1. Ok, this should be easy, googling php twitter api.
  2. Downloading PHP Twitter API Client.
  3. Trying to get the Twitter.php example file to run, replacing all the paths.
  4. Ok runs without errors, now finding where to put in my twitter login data.
  5. Getting some data out after about 10 minutes, but I just want to "getTweats()", looking at getPublicTimeLine(), getArchive(), getMessages, all not what I want.
  6. Ok, finally ran into issue that this API is using a deprecated syntax "archives/..." instead of "statuses", therefore abandoning, FAIL.
  7. Googled and found Retrieving Twitter Status ("Tweets") Via Twitter API.
  8. Ok, just realized that the Twitter API is totally RESTful and hence I can get the XML of my last 10 tweets with this: http://twitter.com/statuses/user_timeline/edward_tanguay.xml?count=10, tweets seem to be called "statuses" officially, ok.
  9. Creating a quick tool for Datapod which will blackbox getting tweets.
  10. Ok, now picking through the XML with simpleXML, ok got it.
  11. Now making the http:// actually links with regex.
  12. Cool, putting the TwitterAPI functionality back into a Datapod 0.51.47.
 
FORAY INTO PHP created on Sunday, January 11, 2009 permalink
Trying to get my web developer site to pull data from the Amazon REST API, SUCCEED
  1. Went to http://www.amazon.com/api, but get a 404 page, companies need to understand that we surf RESTfully now, ok anyway, googling...
  2. Got here http://aws.amazon.com/ looking around, where is REST?
  3. Looking here still can't find REST API, lots of info about the cloud now.
  4. Regoogled, now found some better information.
  5. Ok, seems to all be about the cloud, all I want to do is get informatin about their books via rest, now at http://www.amazon.com.
  6. Ok, googled for amazon associate rest, that's better.
  7. Finally found a page about using Amazon's PHP Web Services With REST.
  8. Making test page with the example on that page.
  9. Signing up for amazon account.
  10. Ok, logged in.
  11. Seems I have to create an AWS account now, ok, doing it, hmmm, it "noticed that I had already created one, ok...".
  12. Seeing if my old code works with the code example.
  13. Ok that code example uses an old version of Amazon's REST API, so now looking at this forum.
  14. Ah, ok, finally a URL example that works.
  15. Wow so how do I just LOG IN to the aws developer site.
  16. Ok logged in after some clicking around.
  17. Cool, now have my access key id, forward progress here.
  18. Now trying to find the syntax to simply look up a book.
  19. Great: here's the URL to get information on my Pro LINQ Object book.
  20. Ok, getting the XML programmatically, blackboxing the CURL call, then I'll figure out how to get the images.
  21. Writing function: getBookInfo($isbn).
  22. getting out book info:
  23. Now figuring out how to get pictures...
  24. Amazing how difficult it is to find examples of how to use the Amazon API, don't they want us to use it? I think there is too much stuff about Amazon Web Services now that it obfuscates the information about their simple REST API for their books.
  25. 10 minutes of googling, the problem is with the above call I get a little information (author, title, publisher) but I want the description and the graphic as well, I remember 3 years ago doing calls which could get various size graphics, all the comments that people left, etc. amazing how hard this is to find now.
  26. Ok, if I can't figure this out in 10 minutes, I'm going to use google's book api instead at least they have findable documentation.
  27. Idea! I'm downloading the code examples from the amazon hacks book from o'reilly.
  28. This book being from 2003, the code examples are old :-(.
  29. Back to my google search, no wait, I'll try the help again at amazon web services account login, yes!, found Amazon Associates Web Service.
  30. this page has some example.
  31. All I want is a list of REST calls and the XML I get back from each, very hard to find.
  32. Their sample doesn't work (has a spelling error on front as well "Eneter key words", ok) but saw they used ItemSearch, trying, no says it needs required parameters, that's why I want to find the documentation.
  33. Trying example from script, says my "account number" is not a valid AWSAccessKeyId.
  34. Amazon's AWS account website is just a frustrating maze of confusion, asking you on almost every page to sign up for some other account, this page: "Sign in to Amazon EC2 Console", what's that?
  35. In a google search foudn this: seems like some useful examples, but it's dated 2005-10-05, why is all the documentation I find so old.
  36. Ok, I'm asking StackOverflow about this...
  37. Found this on another question on StackOverflow, seems to be a What's New is associated with the 2008-08-19 version of the Amazon Associates Web Service, sounds a bit more up-to-date, looking...
  38. This Amazon Associates Web Service Quick Reference Card looks good.
  39. On that PDF you can the syntax you need, e.g. this URL gets images.
  40. So it seems that I can make multiple calls, i.e. 3 calls to get general info, images and description, should be a way to get this all at once, but this is all I need for now, ok, worked.
 
PHP CODE EXAMPLE created on Tuesday, February 03, 2009 permalink
Regex to replace text only outside certain tags
This code shows how to search and replace HTML-coded brackets with actual brackets but not withi PRE tags, e.g. if you want to include code examples between your PRE tags. It is also a nice use of preg_replace_callback.
$text = 'This should be <i>italic</i> but <pre>not <i>this</i> since it is in code</pre> and this should be <i>italic</i> again.';
$newText = preg_replace_callback('%<|>|<pre>.*?</pre>%si', compute_replacement, $text);

echo $newText;

function compute_replacement($groups) {
    if ($groups[0] == '<') {
        return '<';
    } elseif ($groups[0] == '>') {
        return '>';
    } else {
        return $groups[0];
    }
}
 
PHP CODE EXAMPLE created on Tuesday, December 16, 2008 permalink
How to get the base physical path in PHP
Similar to MapPath in ASP.NET.
This line:
echo dirname($_SERVER['SCRIPT_FILENAME']);

returns, e.g. C:/xampplite/htdocs/web/testScripts

which can be used to make sure you are including the correct file no matter where you are in the application, e.g. if you are inside file that renders as a graphic or if you are rendering HTML, etc.
 
PHP CODE EXAMPLE created on Tuesday, August 25, 2009 permalink
How to use Namespaces in PHP 5.3
The new Namespaces feature in PHP 5.3 solves the problem that arises in large PHP application which have dozens of nested classes with the same name which leads to long class names such as Core_Classes_Marketing_PrintManager so that it does not get confused with e.g. PrintManager classes in other directories. Namespaces solves this by allowing each file to define a namespace which allows you to just say "PrintManager" and it looks through all files with the same namespace. The code below also shows you how to use an alias to access classes in namespaces other than the current one.
Here's how you had to do it before:
index.php:
-------------------------------
<?php
include_once('marketing/PrintManager.php');
include_once('sales/PrintManager.php');

$pm1 = new Marketing_PrintManager();
echo $pm1 . '<br/>';

$pm2 = new Sales_PrintManager();
echo $pm2 . '<br/>';
?>

PrintManager.php
------------------------------
<?php
class Marketing_PrintManager {
    function __toString() {
        return 'this is the sales print manager';
    }
}
?>

SalesManager.php
----------------------------------
<?php
class Sales_PrintManager {
    function __toString() {
        return 'this is the sales print manager';
    }
}
?>

And here's how to do it with namespaces:
index.php:
-------------------------------
<?php
namespace Marketing;
use Sales as S;
include 'marketing/PrintManager.php';
include_once('sales/PrintManager.php');

$pm1 = new PrintManager();
echo $pm1 . '<br/>';

$pm2 = new SPrintManager();
echo $pm2 . '<br/>';
?>

PrintManager.php
------------------------------
<?php
namespace Marketing;

class PrintManager {
    function __toString() {
        return 'this is the marketing print manager';
    }
}
?>

SalesManager.php
----------------------------------
<?php
namespace Sales;

class PrintManager {
    function __toString() {
        return 'this is the sales print manager';
    }
}
?>
 
PHP CODE EXAMPLE created on Wednesday, August 26, 2009 permalink
How to use lambdas and closures in PHP 5.3
If you use lambdas in C# or closures in Javascript and want to transfer this approach to your PHP programming, PHP 5.3 now supports this. These examples below show you the syntax to get started, see more examples here.
//lambda with array_filter
$scores = array(89, 87, 65, 98, 100, 99, 87, 82);
$a_scores = array_filter($scores, function($s) { return $s >= 90; });
foreach($a_scores as $a_score) {
    echo $a_score . '<br/>';
}
echo '---<br/>';

//store lambda in variable
$a_score_filter = function($s) { return $s >= 90; };
$scores = array(89, 87, 65, 98, 100, 99, 87, 82);
$a_scores = array_filter($scores, $a_score_filter);
foreach($a_scores as $a_score) {
    echo $a_score . '<br/>';
}
echo '---<br/>';

//closure
$greater_than_score_filter = function($lowestScore) {
    return function($s) use ($lowestScore) { return $s >= $lowestScore; };
};
$scores = array(89, 87, 65, 98, 100, 99, 87, 82);
$ab_scores = array_filter($scores, $greater_than_score_filter(80));
foreach($ab_scores as $ab_score) {
    echo $ab_score . '<br/>';
}
echo '---<br/>';
 
PHP CODE EXAMPLE created on Wednesday, August 26, 2009 permalink
How to use late static binding in PHP 5.3
Before PHP 5.3 it was often confusing that a reference with self:: to a static variable returned the value of that variable in the highest class in the hierarchy, not in the class you had specified. And this is actually correct, since the classes aren't instantiated and in that sense have no "self". PHP 5.3 introduces the "static::" prefix which allows you to solve this as shown below.
class Person {
    static protected $status = 'this is a person';
    static public function getStatus() {
        return self::$status;
    }
}

class Employee extends Person {
    static protected $status = 'this is an employee';
}

echo Employee::getStatus() . '<br/>';

echo '---<br/>';

class Person2 {
    static protected $status = 'this is a person';
    static public function getStatus() {
        return static::$status;
    }
}

class Employee2 extends Person2 {
    static protected $status = 'this is an employee';
}

echo Employee2::getStatus() . '<br/>';
 
PHP CODE EXAMPLE created on Wednesday, August 26, 2009 permalink
How to use the new PHP 5.3 function array_replace
The new function array_replace makes it easy to replace items in an array with groups (arrays) of changes.
$numberNames = array("zero", "UNKNOWN", "two", "UNKNOWN", "four");
$newNumberNames = array(1 => "WAITING", 3 => "three");
$newNumberNames2 = array(1 => "one");

$finalNumberNames = array_replace($numberNames, $newNumberNames, $newNumberNames2);
print_r($finalNumberNames);
 
PHP CODE EXAMPLE created on Wednesday, August 26, 2009 permalink
How to use the new PHP 5.3 function parse_ini_string
The function parse_ini_file is a very useful way to enable an application to hvae a settings file that your website reads from. You can now use parse_ini_string to read ini files out of e.g. the database as strings instead of from files.
//$ini_array = parse_ini_file('test.ini');

$ini = getIniTextFromDatabase();
$ini_array = parse_ini_string($ini);

print_r($ini_array);

echo $ini_array['endRecord'];

function getIniTextFromDatabase() {
    $r = '';
    $newline = "rn";

    $r .= '[general]' . $newline;
    $r .= 'startRecord = 45' . $newline;
    $r .= 'endRecord = 99' . $newline;

    return $r;
}
 
PHP CODE EXAMPLE created on Wednesday, August 26, 2009 permalink
How to use the new PHP 5.3 function date_add
Although this function and its opposite date_sub are still marked as experimental, they can make adding years, days, hours, minutes and seconds to a datetime very easy.
$date = new DateTime("2009-12-31 16:30:30");
echo 'original date: ' . $date->format("Y-m-d H:i:s").'<br />';

date_add($date, new DateInterval("P5D"));
echo 'plus 5 days: ' . $date->format("Y-m-d H:i:s") . '<br/>';

date_add($date, new DateInterval("PT7H"));
echo 'plus 7 hours: ' . $date->format("Y-m-d H:i:s") . '<br/>';

date_add($date, new DateInterval("P2DT5H2M"));
echo 'plus 2 days, 5 hours, and 2 minutes: ' . $date->format("Y-m-d H:i:s") . '<br/>';
 
PHP CODE EXAMPLE created on Wednesday, August 26, 2009 permalink
Three new minor PHP 5.3 functions: gethostname(), lcfirst() and get_called_class()
It's always good to know which useful functions have been added to new language versions. Here are three minor ones I'll be using.
echo gethostname() . '<br/>';

echo convertPascalToCamelNotation('MainLogger') . '<br/>';
function convertPascalToCamelNotation($name) {
    return lcfirst($name);
}

class Person {
    public function initialize() {
        echo get_called_class() . '<br/>';
    }
}

class Employee extends Person {

}

$person = new Person();
$person->initialize();
$employee = new Employee();
$employee->initialize();
 
PHP CODE EXAMPLE created on Friday, August 28, 2009 permalink
How to validate an XML file against an XML Schema (.xsd) file
The following code shows you how to make sure an XML file has specific elements that occur in a specific order and that the data in them is of a specific type. If the XML file is invalid, it will pass the error from the validator function on to the user.
index.php
------------------------
//error_reporting(0);
libxml_use_internal_errors(true); // enable user error handling

$file = 'test.xml';
$schema = 'test.xsd';
$dom = new DOMDocument;
$dom->load($file);


if ($dom->schemaValidate($schema)) {
echo "$file is valid.";
} else {
echo $file    . ' is invalid. The reason is: <span style="color:red">';
$errors = libxml_get_errors();
foreach ($errors as $error) {
//var_dump($error);
echo $error->message;
}
libxml_clear_errors();
echo '</span>';
}

test.xml
--------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>

<shiporder orderid="MS32322">

    <orderperson>John Smith</orderperson>

    <shipto>
        <name>Jim Smith</name>
        <address>1212 Ash St.</address>
        <city>Conwall</city>
        <state>WA</state>
        <zipcode>90283</zipcode>
        <country>USA</country>
    </shipto>

    <item>
        <title>Photoshop Secrets</title>
        <note>Special Edition</note>
        <quantity>1</quantity>
        <price>39.99</price>
    </item>

    <item>
        <title>Photoshop for Beginners</title>
        <quantity>1</quantity>
        <price>29.99</price>
    </item>

    <item>
        <title>Photoshop Tricks</title>
        <quantity>1</quantity>
        <price>19.99</price>
    </item>

</shiporder>

test.xsd
----------------------------------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <!-- definition of simple elements -->
    <xs:element name="orderperson" type="xs:string" />
    <xs:element name="name" type="xs:string" />
    <xs:element name="address" type="xs:string" />
    <xs:element name="city" type="xs:string" />
    <xs:element name="state" type="xs:string" />
    <xs:element name="zipcode" type="xs:string" />
    <xs:element name="country" type="xs:string" />
    <xs:element name="title" type="xs:string" />
    <xs:element name="note" type="xs:string" />
    <xs:element name="quantity" type="xs:positiveInteger" />
    <xs:element name="price" type="xs:decimal" />

    <!-- definition of attributes -->
    <xs:attribute name="orderid" type="xs:integer" />

    <!-- definition of complex elements -->
    <xs:element name="shipto">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="name" />
                <xs:element ref="address" />
                <xs:element ref="city" />
                <xs:element ref="state" />
                <xs:element ref="zipcode" />
                <xs:element ref="country" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="item">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="title" />
                <xs:element ref="note" minOccurs="0" />
                <xs:element ref="quantity" />
                <xs:element ref="price" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="shiporder">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="orderperson" />
                <xs:element ref="shipto" />
                <xs:element ref="item" maxOccurs="unbounded" />
            </xs:sequence>
            <xs:attribute ref="orderid" use="required" />
        </xs:complexType>
    </xs:element>

</xs:schema>
 
PHP CODE EXAMPLE created on Friday, August 28, 2009 permalink
How to check if XML is well-formed
This function checks if an XML string is well-formed, and if not, returns a helpful line which you can pass on to the user to help him find what he needs to correct in his XML file to make it well-formed.
index.php:
---------------------------
$xmlContent = file_get_contents('test.xml');

$errorMessage = '';
if (XmlIsWellFormed($xmlContent, &$errorMessage)) {
    echo 'xml is valid';
    $xml = simplexml_load_string($xmlContent);
    echo '<pre>';
    var_dump($xml);
    echo '</pre>';
} else {
    echo 'xml is not valid, reason: <span style="color:red">' . $errorMessage . '</span>';
}


function XmlIsWellFormed($xmlString, &$message) {
    libxml_use_internal_errors(true);

    $doc = new DOMDocument('1.0', 'utf-8');
    $doc->loadXML($xmlString);

    $errors = libxml_get_errors();
    if (empty($errors))
    {
        return true;
    }

    $error = $errors[ 0 ];
    if ($error->level < 3)
    {
        return true;
    }

    $lines = explode("r", $xmlString);
    $line = $lines[($error->line)-1];

    $message = $error->message . ' at line ' . $error->line . ': ' . htmlentities($line);

    return false;
}

test.xml
-----------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>

<shiporder orderid="MS32322">

    <orderperson>John Smith</orderperson>

    <shipto>
        <name>Jim Smith</name>
        <address>1212 Ash St.</address>
        <city>Conwall</city>
        <state>WA</state>
        <zipcode>90283</zipcode>
        <country>USA</country>
    </shipto>

    <item>
        <title>Photoshop Secrets</title>
        <note>Special Edition</note>
        <quantity>1</quantity>
        <price>39.99</price>
    </item>

    <item>
        <title>Photoshop for Beginners</title>
        <quantity>1</quantity>
        <price>29.99</price>
    </item>

    <item>
        <title>Photoshop Tricks</title>
        <quantity>1</quantity>
        <price>19.99</price>
    </item>

</shiporder>
 
PHP CODE EXAMPLE created on Friday, August 28, 2009 permalink
How to parse XML with XSLT
This example shows you a simple function that enables you to define an XML file and a XSLT file and get a string back which is the parsed result of the two.
index.php
----------------------
echo parseXmlAndXslt('test.xml', 'test.xsl');

function parseXmlAndXslt($xmlFile,$xslFile) {
    $sXml = file_get_contents($xmlFile);
    $XML = new DOMDocument();
    $XML->loadXML( $sXml );

    $xslt = new XSLTProcessor();
    $XSL = new DOMDocument();
    $XSL->load( 'test.xsl', LIBXML_NOCDATA);
    $xslt->importStylesheet( $XSL );

    return $xslt->transformToXML( $XML );
}

test.xml
-------------------------
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<books>
    <specials>
        <special>
            <name>PHP Secrets</name>
            <cost>34.50</cost>
        </special>
        <special>
            <name>Advanced XSLT</name>
            <cost>15.50</cost>
        </special>
        <special>
            <name>The JQuery Bible</name>
            <cost>34.50</cost>
        </special>
        <special>
            <name>Advanced JQuery</name>
            <cost>22.50</cost>
        </special>
    </specials>
</books>

test.xsl
----------------------------
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="html"/>
    <xsl:template match="/">
    The specials of the week are
    <xsl:for-each select="books/specials/special">
            <xsl:value-of select="name"/>
            <xsl:if test="position()!=last()">, </xsl:if>
            <xsl:if test="position()=last()-1">and </xsl:if>
        </xsl:for-each>.
</xsl:template>
</xsl:stylesheet>
 
PHP CODE EXAMPLE created on Friday, August 28, 2009 permalink
How to send in parameters from PHP to an XSLT file
Many times when transforming XML with XSLT, you need to include data that is not in the XML file itself. You can pass this data in from PHP via XSLT parameters as we do with the variable "$timePeriod" in this example.
index.php
----------------------
echo parseXmlAndXslt('test.xml', 'test.xsl');

function parseXmlAndXslt($xmlFile,$xslFile) {
    $sXml = file_get_contents($xmlFile);
    $XML = new DOMDocument();
    $XML->loadXML( $sXml );

    $xslt = new XSLTProcessor();
    $XSL = new DOMDocument();
    $XSL->load($xslFile);
    $xslt->importStylesheet( $XSL );
    $xslt->setParameter('', 'timePeriod', 'week');

    return $xslt->transformToXML($XML);
}

test.xml
--------------------
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<books>
    <specials>
        <special>
            <name>PHP Secrets</name>
            <cost>34.50</cost>
        </special>
        <special>
            <name>Advanced XSLT</name>
            <cost>15.50</cost>
        </special>
        <special>
            <name>The JQuery Bible</name>
            <cost>34.50</cost>
        </special>
        <special>
            <name>Advanced JQuery</name>
            <cost>22.50</cost>
        </special>
    </specials>
</books>

test.xsl
---------------------------
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:param name="timePeriod"/>
    <xsl:output method="html"/>
    <xsl:template match="/">
    The specials of the <xsl:value-of select="$timePeriod"/> are
    <xsl:for-each select="books/specials/special">
            <xsl:value-of select="name"/>
            <xsl:if test="position()!=last()">, </xsl:if>
            <xsl:if test="position()=last()-1">and </xsl:if>
        </xsl:for-each>.
</xsl:template>
</xsl:stylesheet>
 
PHP CODE EXAMPLE created on Friday, August 28, 2009 permalink
How to Use PHP Functions from inside XSLT
Shows how you can easily use any standard PHP function or any custom PHP function from within an XSLT file. Note that when you do this your XSLT is no longer portable to other platforms (e.g. .NET) but this is often just the trick to give your XSLT some flexibility and power that it lacking, especially when you call PHP functions which send a parameter and look information up in a database, etc.
index.php
--------------------------
echo parseXmlAndXslt('test.xml', 'test.xsl');

function parseXmlAndXslt($xmlFile,$xslFile) {
    $sXml = file_get_contents($xmlFile);
    $XML = new DOMDocument();
    $XML->loadXML( $sXml );

    $xslt = new XSLTProcessor();
    $XSL = new DOMDocument();
    $XSL->load($xslFile);
    $xslt->registerPHPFunctions();
    $xslt->importStylesheet( $XSL );
    $xslt->setParameter('', 'timePeriod', 'week');

    return $xslt->transformToXML($XML);
}

class businessLogic {

    public static function addTax($strCost) {
        $intCost = intval($strCost);
        return sprintf('%01.2f', $intCost * 1.06);
    }

}

test.xml
-----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<books>
    <specials>
        <special>
            <name>PHP Secrets</name>
            <cost>10.00</cost>
        </special>
        <special>
            <name>Advanced XSLT</name>
            <cost>15.50</cost>
        </special>
        <special>
            <name>The JQuery Bible</name>
            <cost>34.50</cost>
        </special>
        <special>
            <name>Advanced JQuery</name>
            <cost>22.50</cost>
        </special>
    </specials>
</books>

test.xsl
-----------------------------
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:php="http://php.net/xsl">
    <xsl:param name="timePeriod"/>
    <xsl:output method="html"/>
    <xsl:template match="/">
    The specials of the <xsl:value-of select="$timePeriod"/> are
    <xsl:for-each select="books/specials/special">
            <xsl:value-of select="php:function('strtoupper', string(name))"/>
            (<xsl:value-of select="php:function('businessLogic::addTax', string(cost))"/>)
            <xsl:if test="position()!=last()">, </xsl:if>
            <xsl:if test="position()=last()-1">and </xsl:if>
        </xsl:for-each>.
</xsl:template>
</xsl:stylesheet>
 
PHP CODE EXAMPLE created on Sunday, May 02, 2010 permalink
How to use fopen() to create a proxy site to read any website content into AJAX
If your provider allows fopen() on your hosting server, here is a script that allows you to get data from any website into AJAX, Silverlight, Flash, etc. If your provider doesn't support fopen() you can try this curl version.
<?php
$url = filter_input(INPUT_GET, 'url',FILTER_SANITIZE_STRING);

$validUrlPrefixes[] = "http://tanguay.info";
$validUrlPrefixes[] = "http://www.tanguay.info";

if(beginsWithOneOfThese($url, $validUrlPrefixes)) {
    echo loadFile($url);
} else
    echo "invalid url";

function loadFile($sFilename) {
    if (floatval(phpversion()) >= 4.3) {
        $sData = file_get_contents($sFilename);
    } else {
        if (!file_exists($sFilename)) return -3;                
        
        $rHandle = fopen($sFilename, 'r');
        if (!$rHandle) return -2;

        $sData = '';
        while(!feof($rHandle))
            $sData .= fread($rHandle, filesize($sFilename));
        fclose($rHandle);
    }
    return $sData;
}

function beginsWithOneOfThese($main, $prefixes) {
    foreach($prefixes as $prefix) {
    if(beginsWith($main, $prefix))
        return true;
    }
    return false;
}

function beginsWith($main, $prefix) {
    return strpos($main, $prefix) === 0;
}
?>
 
PHP CODE EXAMPLE created on Sunday, May 02, 2010 permalink
Function to trim the preceding and trailing blank lines off an array
This function uses array_slice to remove the blank lines off the beginning and end of an array, but leaves any internal blank lines. Thanks outis.
<?php

$lines[] = '            ';
$lines[] = ' ';
$lines[] = '';
$lines[] = 'first line';
$lines[] = 'second line';
$lines[] = '';
$lines[] = 'fourth line';
$lines[] = '    ';
$lines[] = '';

$lines = trimLines($lines);
foreach($lines as $line) {
    echo '[' . $line . ']<br/>';
}

function trimLines($lines) {
    $end = count($lines);
    for ($start=0; trim($lines[$start]) === ''; ++$start) {
        if ($start == $end) {
            return array();
        }
    }
    for (--$end; trim($lines[$end]) === ''; --$end) {}
    return array_slice($lines, $start, $end-$start+1);
}
?>
 
PHP CODE EXAMPLE created on Sunday, October 03, 2010 permalink
How to create a singleton in PHP
This example shows the syntax of creating a singleton in PHP. Note that the instance of the class is retrieved twice but its internal variable was set only once which shows that the same instance of the class is being retrieved and not being created again each time it is needed. Use this for any kind of global scope you need in your application, especially classes which have expensive initializations such as retrieving and parsing large amounts of data to be used by various classes in your application.
<?php
echo 'printed at ' . qdat::getMilliseconds() . '<br/>';
$datapodManager = DatapodManager::getInstance();
echo $datapodManager->getTitle() . '<hr/>';


echo 'printed at ' . qdat::getMilliseconds() . '<br/>';
$datapodManager = DatapodManager::getInstance();
echo $datapodManager->getTitle() . '<hr/>';


class DatapodManager {

    protected $title;
    public function getTitle() { return $this->title; }
    
    private static $instance;
    private function __construct() {
        $this->title = 'title set internally at ' . qdat::getMilliseconds();
    }
    
    public static function getInstance(){
        if(!isset(self::$instance)) {
            self::$instance = new DatapodManager();
        }
        return self::$instance;
    }
}

class qdat {
    public static function getMilliseconds() {
        $millisecondsParts = explode ( ' ', microtime () );
        $fraction = $millisecondsParts[0];
        $milliseconds = $fraction * 1000000;
        return $milliseconds;
    }
}
?>
 
PHP CODE EXAMPLE created on Friday, October 29, 2010 permalink
A PHP function which returns the most frequently occuring item
This function shows how you can get the most frequently occurring item out of an array using array_count_values() and arsort().
<?php
//should return "paragraph"
echo getMostFrequentlyOccurringItem(array('line', 'paragraph', 'paragraph')) . '<hr/>';

//should return "line"
echo getMostFrequentlyOccurringItem(array('wholeNumber', 'date', 'date', 'line', 'line', 'line')) . '<hr/>';

//should return null
echo getMostFrequentlyOccurringItem(array('wholeNumber', 'wholeNumber', 'paragraph', 'paragraph')) . '<hr/>';

//should return "wholeNumber"
echo getMostFrequentlyOccurringItem(array('wholeNumber', '', '', '')) . '<hr/>';

function getMostFrequentlyOccurringItem($items) {
    
    //catch invalid entry
    if($items == null) {
        return null;
    }
    if(count($items) == 0) {
        return null;
    }
    
    //sort
    $groups = array_count_values($items);
    arsort($groups);
    
    //if there was a tie, then return null
    $groupAmounts = array_values($groups);
    if($groupAmounts[0] == $groupAmounts[1]) {
        return null;
    }
    
    //get most frequent
    $mostFrequentGroup = '';
    foreach($groups as $group => $numberOfTimesOccurrred) {
        if(trim($group) != '') {
            $mostFrequentGroup = $group;
            break;
        }
    }
    return $mostFrequentGroup;
}

?>
 
PHP CODE EXAMPLE created on Sunday, November 28, 2010 permalink
How to call an unknown function on an unknown class with unknown parameters
The call_user_func_array function can help you build some pretty flexible framework code in PHP, here's the syntax to call a function on an existing object.
<?php

$pm = new PresentationManager();
echo $pm->displayTest('the title', 'the footer', 5);


$params = array( 'the title2', 'the footer2', 10);
echo call_user_func_array(array($pm, 'displayTest'), $params);


class PresentationManager {

    function displayTest($title, $footer, $numberOfItems = 0) {
        $r = '';
        $r .= '<div>'.$title.'</div>';
        for($index = 0; $index < $numberOfItems; $index++) {
            $r .= '<div>the text</div>';
        }
        $r .= '<div>'.$footer.'</div>';
        return $r;
    }
}
?>