Skip to content

Instantly share code, notes, and snippets.

View ThomasWeinert's full-sized avatar
💻
programming

Thomas Weinert ThomasWeinert

💻
programming
View GitHub Profile
@ThomasWeinert
ThomasWeinert / tailrecursion.php
Created November 28, 2012 18:02 — forked from beberlei/tailrecursion.php
PHP Tail Recursion
<?php
class TailRecursion {
private $_function = NULL;
private $_arguments = array();
private $_recursing = FALSE;
private $_recursion = NULL;
public function __construct(Callable $function) {
@ThomasWeinert
ThomasWeinert / xmlToTextWihtAttr.php
Created August 31, 2012 12:33
Convert XML elements into text nodes including attribute values
<?php
$data = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<element>
<title>ein Titel</title>
<content>Ein Inhalt</content>
<content>
Ein Inhalt mit
<subcontent att1="Hallo">Subnodes</subcontent> und weiterem Text,
@ThomasWeinert
ThomasWeinert / xmlToText.php
Created August 31, 2012 09:57
Convert XML elements into text nodes
<?php
$data = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<element>
<title>ein Titel</title>
<content>Ein Inhalt</content>
<content>
Ein Inhalt mit
<subcontent att1="Hallo">Subnodes</subcontent> und weiterem Text,
@ThomasWeinert
ThomasWeinert / array-sort-classes.php
Created May 24, 2012 13:17
Define classes for reusable array sorts
<?php
$data = array(
array('id' => 1, 'title' => 'Top 2'),
array('id' => 2, 'title' => '2. Entry'),
array('id' => 3, 'title' => 'Top 1'),
array('id' => 4, 'title' => '1. Entry')
);
usort(
@ThomasWeinert
ThomasWeinert / xmlToCdata.php
Created April 4, 2012 09:46
Convert XML nodes into CDATA sections
<?php
$data = <<<XML
<sample>
<content>Inhalt Eins</content>
<content>Inhalt <b>Zwei</b></content>
</sample>
XML;
$dom = new DOMDocument();
<?php
/**
* Shows how to highlight several words in the body of a html document. It adds spans with specified
* classes around the found words/word parts.
*
* @version $Id: highlightWords.php 444 2010-05-10 09:29:49Z subjective $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert
*/
<?php
/*
* Sample for Blog Entry:
* http://www.a-basketful-of-papayas.net/2010/04/using-php-dom-with-xpath.html
*/
$errorSetting = libxml_use_internal_errors(TRUE);
$feed = new DOMDocument();
$feed->load('http://www.a-basketful-of-papayas.net/feeds/posts/default');
libxml_clear_errors();
@ThomasWeinert
ThomasWeinert / xpath-context.php
Created August 24, 2015 11:51
Using a callback to fetch the current context in an xpath expression, this might allow to implement the CSS selector `:scope`
<?php
$xml = file_get_contents('php://stdin');
class MyXpath extends DOMXpath {
private static $_context = null;
public function __construct($document) {
parent::__construct($document);
<?php
class MySimpleXMLElement extends SimpleXMLElement {
public function addChild($name, $value = null, $namespace = null) {
$result = parent::addChild($name, null, $namespace);
if (isset($value)) {
$node = dom_import_simplexml($result);
$node->appendChild($node->ownerDocument->createTextNode($value));
}
@ThomasWeinert
ThomasWeinert / hhvm_getter.php
Created February 12, 2015 11:27
Test if HHVM still uses __get/__set or has implemented the properties.
<?php
$xml = <<<'XML'
<root attribute="42"></root>
XML;
$dom = new DOMDocument();
$dom->preserveWhiteSpace = FALSE;
$dom->loadXml($xml);