Skip to content

Collection of small stand-alone PHP scripts for a variety of purposes

AshleyJSheridan/php_snippets

Repository files navigation

php_snippets

What This Is

This is a simple repo to hold a collection of small stand-alone PHP scripts for a variety of purposes.

Contents

(alphabetically)

  1. arduino_php_sockets
  2. head_request_filesize.php
  3. persistent_style_switcher.php
  4. pretty_cli.php
  5. profanity_filter.php
  6. remove_ms_crap.php
  7. reverse_regexp_query_laravel.php
  8. roman_to_numeral_conversion.php
  9. smart_image_resizing.php
  10. uk_postcode_regex.php

Arduino PHP Sockets

This is a simple demo I set up to demonstrate how to communicate between PHP and an Arduino over a USB connection. The PHP script just spits out a letter 'a' once every second continually over the specified port the Arduino is connected to, and the Arduino reads in whatever is in the serial buffer and stores it to an internal string which it builds up.

As the example consists of two scripts in two different languages I put them both into their own directory arduino_php_sockets

Head Request Filesize

Fetches just the header of a remote file and outputs the filesize. In practical terms, and value in the HTTP response headers could be read, the filesize is a useful example.

Persistent Style Switcher

The purpose of this script is to allow your website to utilise several CSS files, that allow a user to pick one that suits them best. This script reads from the $_COOKIE or the $_GET arrays (the latter takes precedence), and looks to see if a style parameter is set. It then outputs all associated stylesheets and marks all but the one specified as an alternate. This has real-world uses for accessibility purposes, where you can give a user a better experience by offering a website style that might better suit them (larger fonts, better contrasting colour scheme, etc) and have it remember their preference.

Pretty CLI

This is a small class intended for CLI scripts, allowing you to set colours for messages and even wrap them in different styles of borders. Screenshot of pretty_cli.php in use

Profanity Filter

This is a profanity filter built with a short base list of words, and some logic in regular expressions to allow for deliberate character replacements of characters that look like letters, and s-p-a-c-e-d o-u-t words that might otherwise not be recognised. It also attempts to look for English pluralisation of words using simple logic. This creates very large regular expressions, so be wary of running it repeatedly over large outut texts. It would be advised to run it over a source text and store that result separately from the original.

Remove MS Crap

Does what it says on the tin. Typically, when content is copy/pasted from Microsoft Office software (Word, Outlook, et al) into a web-based rich text editor, it also copies over lots of unecessary metadata in the form of MS-specific HTML comments, <meta> tags, and other things. This small function helps clean that input up.

Reverse Regular Expression Query for Laravel

This is just a small snippet that you could use with Eloquent (the database abstraction layer used by Laravel and other frameworks) that you could use to query a table with a string, where the table contains the regular expression. This is the reverse of what one normally might do by querying a table of static strings with a regular expression.

Roman to Numeral Conversion

This takes a standard base-10 integer and converts it to a Roman-numeral string using the traditional syntax (so 4 is ⅠⅤ and not ⅠⅠⅠⅠ)

Smart Image Resizing

This function creates a resized image from a JPEG. The smart part of this is how it performs cropping where necessary, as it looks at the orientation of the source image to determine what parts are more likely to be fine to crop out. So a portrait image, for example, would crop out the bottom area of the image if it was being resized to a square image.

UK Postcode Regex

This is just a regular expression built to match a valid UK postcode according to the format specified by https://www.mrs.org.uk/pdf/postcodeformat.pdf . One thing to note is that this doesn't accept short-form postcodes, as they are technically not valid according to the spec. If that is what you need, then you can make the latter half of the regex optional with the ? modifier.

About

Collection of small stand-alone PHP scripts for a variety of purposes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages