Skip to content

Instantly share code, notes, and snippets.

View stemar's full-sized avatar

Steven Marshall stemar

View GitHub Profile
@stemar
stemar / trim.js
Created March 16, 2015 23:03
trim.js
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, "");
};
@stemar
stemar / timezone_abbreviation.js
Last active April 14, 2021 18:01
Get timezone abbreviation from date object or date string
var timezoneAbbreviation = function(timezoneString) {
var timezoneAbbreviations = {
"Afghanistan Time": "AFT",
"Alaska Daylight Time": "AKDT",
"Alaska Standard Time": "AKST",
"Amazon Summer Time": "AMST",
"Amazon Time": "AMT",
"Arabia Standard Time": "AST",
"Argentina Time": "ART",
"Armenia Summer Time": "AMST",
@stemar
stemar / filter_html_attribute.rb
Last active September 15, 2019 04:34
Ruby helpers to filter out unwanted HTML class names or style properties, including example to filter out Microsoft Office classes and styles
class String
def filter_html_attribute(attribute_key, args={})
self.gsub!(/#{attribute_key}="([^"]*)"/) do |attribute|
return "" if attribute.nil?
attribute_value = $1.split(args[:split]).delete_if {|i| i.match(args[:match]) }.join(args[:join])
attribute_value += args[:append].to_s unless attribute_value.empty?
attribute.replace("#{attribute_key}=\"#{attribute_value}\"")
end
self.gsub!(/ (#{attribute_key})=""/, "")
end
@stemar
stemar / p.rb
Last active September 15, 2019 04:31
Replace a string's placeholders with the provided key/value pairs
class String
def p(**args)
gsub(/:[\w]*/){|match| args[match[1..-1].to_sym] || match}
end
end
@stemar
stemar / preg_grep_keys.php
Last active December 7, 2019 00:04
Filter in or out keys with a regex pattern
<?php
/**
* Get array entries that match pattern in their keys.
*
* @link https://www.php.net/manual/en/function.preg-grep.php
* @param string $pattern
* @param array $assoc
* @param int $flags 0 or PREG_GREP_INVERT
* @return array
*/
@stemar
stemar / format_xml.php
Last active September 15, 2019 04:28
Format an unformatted XML string or unformat a formatted XML string, also accepts SimpleXMLElement xml object.
<?php
function format_xml($xml, $formatOutput=TRUE, $declaration=TRUE) {
$sxe = ($xml instanceof \SimpleXMLElement) ? $xml : simplexml_load_string($xml);
$domElement = dom_import_simplexml($sxe);
$domDocument = $domElement->ownerDocument;
$domDocument->preserveWhiteSpace = false;
$domDocument->formatOutput = (bool)$formatOutput;
$domDocument->loadXML($sxe->asXML(), LIBXML_NOBLANKS); // Fixes newlines omitted by DomNode::appendChild()
return (bool)$declaration ? $domDocument->saveXML() : $domDocument->saveXML($domDocument->documentElement);
}
@stemar
stemar / xml_to_json.php
Last active September 15, 2019 04:26
Convert XML string to JSON string, formatted or not.
<?php
function xml_to_json($xml, $formatted=TRUE) {
$options = (bool)$formatted ? JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE : 0;
return json_encode(simplexml_load_string($xml), $options);
}
@stemar
stemar / loadHTML.php
Last active September 23, 2016 03:33
DOMDocument::loadHTML() removing invalid tags without adding DOCTYPE and <html> tag
<?php
// Invalid end tag </s>
$html = <<<HTML
<table>
<tr>
<td>123</s></td>
<td>456</td>
</tr>
</table>
HTML;
@stemar
stemar / xml_to_array.php
Last active December 19, 2024 23:28
Convert XML to an array and convert the SimpleXMLElement empty arrays into empty strings.
<?php
function xml_to_array($xml) {
return json_decode(str_replace(['{}', '"NULL"'], ['""', 'null'], json_encode(simplexml_load_string($xml))), TRUE);
}