Skip to content

Instantly share code, notes, and snippets.

View joshuabaker's full-sized avatar
🎭

Joshua Baker joshuabaker

🎭
View GitHub Profile
@joshuabaker
joshuabaker / gist:313648
Last active May 11, 2022 07:17
Get all images in a HTML string
<?php
/**
* Returns all img tags in a HTML string with the option to include img tag attributes
*
* @author Joshua Baker
*
* @example $post_images[0]->html = <img src="example.jpg">
* $post_images[0]->attr->width = 500
*
/**
* jQuery.rgbToHex - Converts an RGB string to a HEX string (forces length 6)
* @author Joshua Baker
* @version 1.0.0
*/
;(function($){
$.extend({
rgbToHex: function(rgbString) {
var parts = rgbString.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
if ( ! parts) {
@joshuabaker
joshuabaker / is_mobile.php
Created December 6, 2010 17:12
Detects whether a visitor is using a mobile device. This, although short, caters for all of the mobile device user agents listed on Wikipedia. Please first consider WURFL before using this in production.
function is_mobile()
{
return preg_match('/(ACER\ E101|Android|BlackBerry7230|BlackBerry7730|Blazer|LG\/U8120|LG\/U8130|LG\/U8138|LG\/U8180|LG\/U880|MIDP-2\.|Maemo\ Browser|MobilePhone|NetFront|Obigo|ObigoInternetBrowser|Opera\ Mini|SAMSUNG-SGH-i900|SymbianOS|UP\.Browser|WAP|Windows\ CE\;\ IEMobile|Windows\ CE\;\ PPC|Windows\ Phone|acer_S200|iPhone|iPod|webOS)/i', $_SERVER['HTTP_USER_AGENT']);
}
@joshuabaker
joshuabaker / odd_even_num.php
Created March 30, 2011 15:53
Detect if a number is odd or even.
$number = 10;
echo ($number & 1) ? "odd" : "even";
@joshuabaker
joshuabaker / zero_fill.php
Created April 4, 2011 14:38
Pad a string or zero fill a number using PHP.
<?php
$number = 5;
echo sprintf('%02s', $number); // 05
echo sprintf('%03s', $number); // 005
echo sprintf('%02s', 453); // 453
// sprintf is faster than str_pad
@joshuabaker
joshuabaker / jquery.fn.invert.js
Created April 25, 2011 15:05
Invert filter method for jQuery
$.fn.invert = function () { return this.end().not(this); };
$.fn.inlineCSS = function() {
var self, style, prop, props;
return this.each(function() {
self = $(this);
style = this.style;
props = [];
for (prop in style) {
if (self.css(prop))
props.push(prop + ':' + self.css(prop));
@joshuabaker
joshuabaker / gist:1217961
Created September 14, 2011 22:15
hereDoc hack for javascript
function hereDoc(f) {
return f.toString().
replace(/^[^\/]+\/\*!?/, '').
replace(/\*\/[^\/]+$/, '');
}
// usage
var tennysonQuote = hereDoc(function() {/*!
Theirs not to make reply,
@joshuabaker
joshuabaker / gist:1339648
Created November 4, 2011 15:46
Parse Tweet
function parseTweet(str) {
return str
.replace(/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi, '<a href="$1" target="_blank" class="tweet-link">$1</a>')
.replace(/(^|[\W])@(\w+)/gi, '$1@<a href="http://twitter.com/$2">$2</a>')
.replace(/\#([a-zA-Z0-9-_]+)/gi, '<a href="http://twitter.com/search/%23$1" target="_blank" class="tweet-hash">#$1</a>');
}
@joshuabaker
joshuabaker / responsive.css
Created March 28, 2012 17:08
Media queries for responsive layouts
// Responsive.css
// -------------------------------------------------------------
// UP TO LANDSCAPE PHONE
// ---------------------
@media (max-width: 480px) {}