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 / php-version.bat
Created March 13, 2021 17:14
Allow for switch between multiple PHP versions. Expects the PHP version to be in subdirectories `PHP_%VERSION%` (PHP_8.0.4).
@echo off & setlocal enabledelayedexpansion
IF "%~1"=="" GOTO Help
IF "%~1"=="list" GOTO ListVersion
IF "%~1"=="bat" GOTO CreateBatchFiles
:LINK_VERSION
set PHP_VERSION=%1
set PHP_BASE_PATH=%~dp0
set "PHP_BINARY=%~dp0PHP_%PHP_VERSION%\php"
@ThomasWeinert
ThomasWeinert / xml-namespace-read.php
Created November 1, 2019 16:25
How to read XML with namespaces
<?php
// define the namespace URI as a constant
// this is often an URL but it does not have to be one
// in existing XMLs look for the "xmlns/xmlns:*" attributes
const XMLNS_EXAMPLE = 'urn:example-namespace';
// load a really basic XML with the example namespace
$document = new DOMDocument();
$document->loadXML('<f:foo xmlns:f="urn:example-namespace">content</f:foo>');

libary.xml

<project name="buildlibrary" default="hello">
  <target name="hello" hidden="yes">
    <echo message="Hello World!"/>
  </target>
</project>
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="150" height="100">
<defs>
<filter id="b">
<feGaussianBlur stdDeviation="12"/>
</filter>
<linearGradient id='spinner'>
<stop stop-color='rgba(255, 255, 255, 0)'/>
<stop offset='0.2' stop-color='rgba(255, 255, 255, 0)'/>
<stop offset='0.5' stop-color='#fff'/>

Keybase proof

I hereby claim:

  • I am thomasweinert on github.
  • I am thomasweinert (https://keybase.io/thomasweinert) on keybase.
  • I have a public key ASBwbCBQ_ZVJ6zqKiD0Bb3WkLLf4Rz6VObXll9dMYggZXwo

To claim this, I am signing this object:

@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);
@ThomasWeinert
ThomasWeinert / domtest.php
Created February 11, 2015 09:24
FluentDOM\Document - extends DOMDocument
<?php
require_once(__DIR__.'/vendor/autoload.php');
function test_fd() {
$html = file_get_contents(__DIR__.'/data.html');
$html = '<?xml encoding="utf-8"?>'.$html;
$dom = FluentDOM::load($html, 'text/html');
$r = array();
var XMLCreatorFactory = function(dom, resolver) {
if (dom === true) {
dom = document.implementation.createDocument('', '', null);
} else if (!(dom instanceof Document)) {
dom = document;
}
var lookupNamespaceURI;
if (resolver instanceof Function && !resolver.lookupNamespaceURI instanceof Function) {
lookupNamespaceURI = resolver;