Skip to content

Instantly share code, notes, and snippets.

View stemar's full-sized avatar

Steven Marshall stemar

View GitHub Profile
@stemar
stemar / blank.rb
Last active May 23, 2016 23:01
blank? method in Ruby
def blank?
self.respond_to?(:empty?) ? self.empty? : !self
end
@stemar
stemar / is_blank.py
Last active October 1, 2015 12:48
is_blank() method in Python
def is_blank(value):
return False if value is not False and value in [0, 0.0] else not value
@stemar
stemar / a.php
Last active September 15, 2019 04:37
A PHP function to create an <a> link and a callback to join HTML tag attributes.
<?php
function a($href, $text=NULL, $attributes=NULL) {
if (!isset($text))
$text = strpos((string)$href, 'mailto:') !== FALSE ? substr($href, 7) : $href;
$attributes = compact('href') + (array)$attributes;
return sprintf('<a%s>%s</a>', join_attributes($attributes), (string)$text);
}
function join_attributes($attributes) {
$attrs = array();
foreach ((array)$attributes as $key => $value)
@stemar
stemar / hsc.php
Last active May 23, 2016 22:47
A UTF-8 compatible htmlspecialchars() with optional arguments. An example of a function with optional arguments.
<?php
function hsc($value, $args=NULL) {
$default_args = array('flags'=>ENT_COMPAT, 'encoding'=>'UTF-8', 'double_encode'=>FALSE);
extract(array_merge($default_args, (array)$args));
return htmlspecialchars((string)$value, (int)$flags, (string)$encoding, (bool)$double_encode);
}
@stemar
stemar / array_select.php
Last active December 7, 2019 00:06
Select a subset of an associative array by providing the keys.
<?php
/**
* Select a subset of an associative array by providing the keys.
*
* @param array $assoc
* @param array $keys
* @return array
*/
function array_select(array $assoc, array $keys) {
return array_intersect_key($assoc, array_flip($keys));
@stemar
stemar / array_remove.php
Last active December 7, 2019 00:06
Remove a subset of an associative array by providing the keys.
<?php
/**
* Remove a subset of an associative array by providing the keys.
*
* @param array $assoc
* @param array $keys
* @return array
*/
function array_remove(array $assoc, array $keys) {
return array_diff_key($assoc, array_flip($keys));
@stemar
stemar / mb_substr_replace.php
Last active December 24, 2022 19:41
Multibyte substr_replace(). The mbstring library PHP < 7.x doesn’t come with a multibyte equivalent of substr_replace(). This function behaves exactly like substr_replace() even when the arguments are arrays.
<?php
function mb_substr_replace($string, $replacement, $start, $length=NULL) {
if (is_array($string)) {
$num = count($string);
// $replacement
$replacement = is_array($replacement) ? array_slice($replacement, 0, $num) : array_pad(array($replacement), $num, $replacement);
// $start
if (is_array($start)) {
$start = array_slice($start, 0, $num);
foreach ($start as $key => $value)
@stemar
stemar / gist:327e34a0b2bebd11adb5
Last active January 18, 2024 10:50
Adding target="_blank" or any other attribute to a Markdown link
Markdown:
[JP Markdown](https://wordpress.org/plugins/jetpack-markdown){.target-blank .rel-nofollow}
JavaScript (just before the closing </body> tag):
<script>
jQuery('.target-blank').attr('target', '_blank').removeClass('target-blank').filter('[title=""]').removeAttr('title').filter('[class=""]').removeAttr('class');
jQuery('.rel-nofollow').attr('rel', 'nofollow').removeClass('rel-nofollow').filter('[title=""]').removeAttr('title').filter('[class=""]').removeAttr('class');
</script>
Post-jQuery HTML:
@stemar
stemar / p.js
Last active August 29, 2015 14:16
JavaScript function that replaces named placeholders in a string template. http://jsfiddle.net/mobc4vss/8/
// Usage: "abc{def}ghi".p({def:"xyz"}); => "abcxyzghi"
String.prototype.p = function(obj) {
return this.replace(/\{[^\}]+\}/g, function(key) {
return obj[key.replace(/^\{([^\}]+)\}$/, "$1")] || key;
});
};
@stemar
stemar / JavaScript window.location
Last active August 29, 2015 14:16
Extend JavaScript window.location with 2 properties
window.location.href returns the URL of the current page
http://www.example.org:8888/foo/bar/index.html?q=baz&w=io#bang
window.location.origin returns the protocol, hostname and port number of a URL
http://www.example.org:8888
window.location.protocol returns the web protocol used (http:// or https://)
http:
window.location.hostname returns the domain name of the web host