Skip to content

Instantly share code, notes, and snippets.

View lsloan's full-sized avatar

Mr. Lance E Sloan «UMich» lsloan

  • Teaching and Learning (@tl-its-umich-edu) at University of Michigan: Information and Technology Services
  • Ann Arbor, Michigan, USA
  • 11:21 (UTC -04:00)
  • X @lsloan_umich
View GitHub Profile
<?php
// Downloaded from: https://code.google.com/archive/p/php-string/downloads
// (Specifically: https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/php-string/string.php)
/**
* String class for PHP that provides convenient methods for handling multibyte strings.
*
* The class supports the extensions mbstring and iconv, and the package
* PHP-UTF8. It chooses the best available function for each method.
* In addition, it provides many new methods. Some of them are:
@lsloan
lsloan / OAuth_example.php
Last active May 19, 2016 17:15
An example of using a popular PHP OAuth package (eher/oauth) to generate the value of the HTTP Authorization header for a request.
<?php
require_once 'vendor/autoload.php';
/**
* Why doesn't PHP already have these defined?
*/
class HttpMethods {
const
HTTP_METHOD_DELETE = 'DELETE',
HTTP_METHOD_GET = 'GET',
@lsloan
lsloan / iso8601TimestampWithMilliseconds.php
Last active January 17, 2023 07:40
Get PHP DateTime objects with fractional seconds and format them according to ISO 8601.
/*
* The `DateTime` constructor doesn't create objects with fractional seconds.
* However, the static method `DateTime::createFromFormat()` does include the
* fractional seconds in the object. Finally, since ISO 8601 specifies only
* millisecond precision, remove the last three decimal places from the timestamp.
*/
// DateTime object with microseconds
$now = DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', ''));
<a href="http://lemonly.com/work/celebrating-towel-day-may-25-2012" title="Don't Panic, Use a Towel Day Infographic">
<img src="http://lemonly.com/wp-content/uploads/2012/05/Towel-day-Infographic2.jpg"style="max-width: 100%" alt="Don't Panic, Use a Towel Day Infographic; Design by Lemonly" />
</a>
Learn more about <a href="http://lemonly.com/work/celebrating-towel-day-may-25-2012">Towel Day</a> and <a href="http://lemonly.com">Infographic Design</a> from Lemonly.
<a href="http://lemonly.com/work/42-towel-day-infographic" title="42: Life, the Universe and Everything – A Towel Day Infographic by Lemonly">
<img src="http://lemonly.com/wp-content/uploads/2013/05/Towel_Day-42-Infographic-960x2178.jpg"style="max-width: 100%" alt="42: Life, the Universe and Everything – A Towel Day Infographic Design by Lemonly" />
</a>
Learn more about <a href="http://lemonly.com/work/42-towel-day-infographic">Towel Day </a> and <a href="http://lemonly.com">Infographic Design</a> from Lemonly.
@lsloan
lsloan / numericStringArrayKeys.php
Created May 29, 2016 15:39
PHP converts strings of numeric characters to numbers when used as array indices or keys. Some tricks to get around that.
<?php
$key = '11'; // A key to be used in both examples
// Usual behavior: numeric strings are converted to numbers
$data = [];
$data[$key] = 38;
$data['12'] = 37;
echo var_export($data, 'return') . PHP_EOL; // notice keys are numbers
echo var_export(array_key_exists('12', $data), 'return') . PHP_EOL; // true
echo $data['12'] . PHP_EOL; // 37
@lsloan
lsloan / invalid_composer.json
Last active June 2, 2016 22:00
Invalid JSON: This JSON is invalid because it contains multiple values for "url" in "repositories". Many browsers will keep only one value, the second "url" key, accessible as x.repositories[0].url. See: https://jsfiddle.net/lsloan/xu2c0tbd/
{
"repositories":[
{
"type":"vcs",
"url":"https://github.com/gy-vgf-hzvpu-rqh/pnyvcre-cuc-choyvp",
"url":"https://github.com/gy-vgf-hzvpu-rqh/ivnqhgbb"
}
],
"require":{
"php":">=5.4",
@lsloan
lsloan / ordered_json.py
Created June 8, 2016 20:37 — forked from onyxfish/ordered_json.py
Usage of object_pairs_hook to load JSON with guaranteed key order
#!/usr/bin/env python
from collections import OrderedDict
import json
write_data = OrderedDict([
('a', '1'),
('b', '2'),
('c', '3')
])
@lsloan
lsloan / DataHolder.py
Last active June 9, 2016 15:38
DataHolder.py, allow assignment in conditionals. From: http://code.activestate.com/recipes/66061/
# When transliterating C, Perl &c to Python, one often misses idioms such
# as "if((x=foo())" or "while((x=foo())".
# In Python, you can't code "if x=foo():" -- assignment is a statement, thus
# you can't fit it into an expression, as needed for conditions of if and
# while statements, &c. No problem, if you just structure your code around
# this. But sometimes you're transliterating C, or Perl, or ..., and you'd
# like your transliteration to be structurally close to the original.
#
# No problem, again! One tiny, simple utility class makes it easy...:
@lsloan
lsloan / prompt_escape_sequences.md
Last active June 10, 2016 15:15
Notes about bash prompt settings and terminal escape sequences.