Skip to content

Instantly share code, notes, and snippets.

View ThomasWeinert's full-sized avatar
💻
programming

Thomas Weinert ThomasWeinert

💻
programming
View GitHub Profile
<?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();
<?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
*/
@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();
@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 / 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 / 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 / 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 / groupedmax.sql
Created November 29, 2012 11:37
SQL Maximum Of Group (latest ten, but only one of each group)
SELECT *
FROM mytable
WHERE idfield IN (
SELECT MAX(main.idfield)
FROM mytable AS main
JOIN (
SELECT groupfield, MAX(maxfield) maximum
FROM mytable
GROUP BY groupfield
ORDER BY maximum DESC
@ThomasWeinert
ThomasWeinert / gist:4500763
Last active December 10, 2015 22:18 — forked from tliff/gist:4500652
<?php
$data = '<div>blabla<h1><a href="http://www.example.com/blabla.php?id=12345" class=l>blub</a></h1><h2><a href="http://www.example.com/asfadfafadf.php?id=12345" class=l>blub</a></h2>sdfafgdghaghagh<h1><a href="http://www.example.com/fdgadfhfh.php?id=123dsfasdf45" class=l>blub</a>asdfasdfasdf</h1>';
$doc = new DOMDocument();
$doc->loadHTML($data);
$xpath = new DOMXpath($doc);
$linkTargets = array();
foreach ($xpath->evaluate('//*[@href]/@href', NULL, FALSE) as $attribute) {
$linkTargets[] = $attribute->value;
@ThomasWeinert
ThomasWeinert / async-mysql.php
Created May 30, 2013 13:27
Asynchronous MySQL queries
<?php
include('../../src/Carica/Io/Loader.php');
Carica\Io\Loader::register();
use Carica\Io;
$mysqlOne = new Io\Deferred\MySQL(new mysqli('127.0.0.1'));
$mysqlTwo = new Io\Deferred\MySQL(new mysqli('localhost'));
$time = microtime(TRUE);