Skip to content

Instantly share code, notes, and snippets.

@lutzissler
lutzissler / wpautop.inc.php
Created June 10, 2022 11:42
Self-contained copy of wpautop() to include in migration scripts etc.
<?php
function _autop_newline_preservation_helper( $matches ) {
return str_replace( "\n", '<WPPreserveNewline />', $matches[0] );
}
function get_html_split_regex() {
static $regex;
if ( ! isset( $regex ) ) {
@lutzissler
lutzissler / php_timezone_list.txt
Created February 10, 2022 08:50
Output of timezone_identifiers_list() from PHP 7.4.25 (cli) (built: Oct 21 2021 07:37:05) ( NTS ).
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
@lutzissler
lutzissler / func.encode_url.inc.php
Created September 16, 2020 10:30
Encode a URL (or just a path) with `rawurlencode()` to make it pass the W3C validator in HTML source and, of course, be a valid URL.
function encode_url($url) {
$parsed_url = parse_url($url);
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
@lutzissler
lutzissler / get_embed_url.php
Created July 16, 2020 12:13
Return the proper iframe URL for any YouTube, Vimeo, or SoundCloud URL. $cache is a PhpFastCache instance (but not required).
function get_embed_url($url) {
global $cache;
// Check for YouTube
$matchFound = preg_match('/^(https?:\/\/(www\.)?youtube\.com\/watch\?(t=(.+)&)?v=|https?:\/\/youtu\.be\/)([^?]+)(\?(t=(.+))?.*)?$/', $url, $matches);
if ($matchFound) {
// Video is a Youtube video
$url = '//www.youtube.com/embed/' . $matches[5];
if ($matches[4]) {
$url = $url . '?start=' . $matches[4];
@lutzissler
lutzissler / function.my_getimagesize.php
Created September 26, 2019 08:54
Get image size in PHP respecting EXIF rotation.
function my_getimagesize($filename) {
$size = @getimagesize($filename);
if (is_array($size) && function_exists('exif_read_data')) {
$exif = @exif_read_data($filename);
if (is_array($exif) && isset($exif['Orientation']) && ($exif['Orientation'] == 6 || $exif['Orientation'] == 8)) {
// The image is tilted clockwise or counter-clockwise
// Flip width and height
$x = $size[0];
$size[0] = $size[1];
$size[1] = $x;
@lutzissler
lutzissler / function.closetags.php
Created December 18, 2018 22:54
Close any open tags in HTML string
function closetags($html) {
libxml_use_internal_errors(true);
$dom = new \DOMDocument;
$dom->loadHTML($html);
// Strip wrapping <html> and <body> tags
$mock = new \DOMDocument;
$body = $dom->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $child) {
@lutzissler
lutzissler / func.multiple_strftime.php
Last active August 15, 2021 20:17
Works like strftime() but allows for multiple occurences of the formatting tokens, just like sprintf(). Also, add capability for 'O' (uppercase letter “oh”) which creates an ordinal day suffix, 'K' for the shorted 24 hour format possible ('8' instead of '08:00', '12' for '12:00', '12:15' for '12:15'), 'L' for the shortest 12 hour format possible…
function multiple_strftime($format) {
if (func_num_args() < 2) {
return $format;
}
$args = func_get_args();
return strftime(preg_replace_callback('/%(\d)\$(.)/', function ($match) use ($args) {
if (isset($args[$match[1]])) {
if ($match[2] == 'O') {
// Ordinal day suffix
return date('S', $args[$match[1]]);
@lutzissler
lutzissler / func.encode_mailto.inc.php
Last active June 10, 2016 06:36
ROT13 encode all mailto: links in the HTML.
function encode_mailto($s) {
return preg_replace_callback('#<a href="mailto:(.+?)">(.*?)</a>#', function ($matches) {
return '<a href="#" onclick="location.href=\'' . str_rot13('mailto:' . $matches[1]) . '\'.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<=\'Z\'?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});return false">' . str_replace('@', ' [&#8203;at&#8203;] ', $matches[2]) . '</a>';
}, $s);
}
@lutzissler
lutzissler / countries.inc.php
Last active September 21, 2015 08:48
Alphabetical list of all countries, in English and German.
<?php
$countries = array(
'en' => array(
'Afghanistan',
'Albania',
'Algeria',
'American Samoa',
'Andorra',
'Angola',
@lutzissler
lutzissler / func.flushDOM.js
Created August 4, 2015 11:23
Workaround for forcing the browser to write any cached changes to the DOM.
function flushDOM() {
$('body').height();
}