Terence Eden. He has a beard and is smiling.

Terence Eden’s Blog

Theme Switcher:

PHP - simple way to send HTTP headers before a script ends

· 1 comment · 50 words · Viewed ~1,460 times


The PHP logo.

Suppose you want PHP to keep processing after it has sent back an HTTP response. Normally, this doesn't work: <?php header( "Location: https://example.com/" ); // Long operation. sleep(10); die(); Try it yourself. You'll have to wait 10 seconds before you get back < HTTP/2 302 < location: https://example.com/ There are some complex ways to fix this - they usually involve…

Vertically Aligning Roman Numerals in Code

· 1 comment · 200 words · Viewed ~2,287 times


The PHP logo.

I have a PHP function which uses Roman Numerals. It looks like this: $romanNumerals = [ "Ⅿ" => 1000, "ⅭⅯ" => 900, "Ⅾ" => 500, "ⅭⅮ" => 400, "Ⅽ" => 100, "ⅩC" => 90, "Ⅼ" => 50, "ⅩⅬ" => 40, "Ⅹ" => 10, "Ⅸ" => 9, "Ⅷ" => 8, "Ⅶ" => 7, "Ⅵ" => 6, "Ⅴ" => 5, "Ⅳ" => 4, "Ⅲ" => 3, "Ⅱ" => 2, "Ⅰ" => 1 ]; The problem is, the…

You can parse an .env file as an .ini with PHP - but there's a catch

· 1 comment · 500 words · Viewed ~2,024 times


The PHP logo.

The humble .env file is a useful and low-tech way of storing persistent environment variables. Drop the file on your server and let your PHP scripts consume it with glee. But consume it how? There are lots of excellent parsing libraries for PHP. But isn't there a simpler way? Yes! You can use PHP's parse_ini_file() function and it works. But… .env and .ini have subtly different behaviour which …

Some updates to ActivityBot

· 1 comment · 1,150 words · Viewed ~576 times


Logo for ActivityPub.

I couple of years ago, I developed ActivityBot - the simplest way to build Mastodon Bots. It is a single PHP file which can run an entire ActivityPub server and it is less than 80KB. It works! You can follow @openbenches@bot.openbenches.org to see the latest entries on OpenBenches.org, and @colours@colours.bots.edent.tel for a slice of colour in your day, and @solar@solar.bots.edent.tel to see…

A big list of things I disable in WordPress

· 14 comments · 900 words · Viewed ~4,419 times


The Logo for WordPress.

There are many things I like about the WordPress blogging software, and many things I find irritating. The most annoying aspect is that WordPress insists that its way is the best and there shall be no deviance. That means a lot of forced cruft being injected into my site. Headers that bloat my page size, Gutenberg stuff I've no use for, and ridiculous editorial decisions. To double-down on the…

A Self-Hosted Favicon Proxy written in PHP

· 3 comments · 600 words · Viewed ~509 times


The PHP logo.

In theory, you should be able to get the base favicon of any domain by calling /favicon.ico - but the reality is somewhat more complex than that. Plenty of sites use a wide variety of semi-standardised images which are usually only discoverable from the site's HTML. There are several services which allow you to get favicons based on a domain. But they all have their problems. Google Exposes…

Stop using preg_* on HTML and start using \Dom\HTMLDocument instead

· 5 comments · 850 words · Viewed ~1,039 times


The PHP logo.

It is a truth universally acknowledged that a programmer in possession of some HTML will eventually try to parse it with a regular expression. This makes many people very angry and is widely regarded as a bad move. In the bad old days, it was somewhat understandable for a PHP coder to run a quick-and-dirty preg_replace() on a scrap of code. They probably could control the input and there wasn't …

Using Tempest Highlight with WordPress

· 1 comment · 300 words · Viewed ~276 times


The HTML5 Logo.

I like to highlight bits of code on my blog. I was using GeSHi - but it has ceased to receive updates and the colours it uses aren't WCAG compliant. After skimming through a few options, I found Tempest Highlight. It has nearly everything I want in a code highlighter:  PHP with no 3rd party dependencies.  Lots of common languages.  Modern, with regular updates.  Easy to use fun…

A small PHP update to GeSHi

· 250 words · Viewed ~231 times


The PHP logo.

The faithful old GeSHi Syntax Highlighter hasn't seen an update in a many a long year. It's a tried and trusted way to do server-side code highlighting - turning a myriad of programming languages into beautiful HTML & CSS. A few weeks ago, I noticed someone had proposed an update to its HTML rendering. The changes were mostly adding in new element names. PHP has been updated several times…

Introducing Pretty Print HTML for PHP 8.4

· 550 words · Viewed ~628 times


The HTML5 Logo.

I'm delight to announce the first release of my opinionated HTML Pretty Printer for new versions of PHP. Grab the code from Packagist Contribute on GitLab There are several prettifiers on Packagist, but I think mine is the only one which works with the new Dom\HTMLDocument class. Table of ContentsWhatHowLimitationsWhyNext Steps What This takes hard-to-read HTML like: <!doctype…

An opinionated HTML Serializer for PHP 8.4

· 800 words · Viewed ~991 times


The HTML5 Logo.

A few days ago, I wrote a shitty pretty-printer for PHP 8.4's new Dom\HTMLDocument class. I've since re-written it to be faster and more stylistically correct. It turns this: <html lang="en-GB"><head><title id="something">Test</title></head><body><h1 class="top upper">Testing</h1><main><p>Some <em>HTML</em> and an <img src="example.png" alt="Alternate Text"></p>Text not in an…

Pretty Print HTML using PHP 8.4's new HTML DOM

· 1 comment · 950 words · Viewed ~834 times


The PHP logo.

Those whom the gods would send mad, they first teach recursion. PHP 8.4 introduces a new Dom\HTMLDocument class it is a modern HTML5 replacement for the ageing XHTML based DOMDocument. You can read more about how it works - the short version is that it reads and correctly sanitises HTML and turns it into a nested object. Hurrah! The one thing it doesn't do is pretty-printing. When you call…