Skip to content

Instantly share code, notes, and snippets.

View webmechanicx's full-sized avatar
🎯
reinventing

Farhadur Rahim webmechanicx

🎯
reinventing
View GitHub Profile
@webmechanicx
webmechanicx / getDateDiff.php
Created June 25, 2016 01:30
Get human readable time difference between two given dates
/* @param mixed $time1 a time (string or timestamp)
* @param mixed $time2 a time (string or timestamp)
* @param integer $precision Optional precision
* @return string time difference
*/
function get_date_diff( $time1, $time2, $precision = 2 ) {
// If not numeric then convert timestamps
if( !is_int( $time1 ) ) {
// encode(decode) html text into html entity
var decodeHtmlEntity = function(str) {
return str.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
});
};
var encodeHtmlEntity = function(str) {
var buf = [];
for (var i=str.length-1;i>=0;i--) {
@webmechanicx
webmechanicx / gist:b33a99bbdcef860906e7
Created March 27, 2016 22:44 — forked from jonathanmoore/gist:2640302
Get the share counts from various APIs

Share Counts

I have always struggled with getting all the various share buttons from Facebook, Twitter, Google Plus, Pinterest, etc to align correctly and to not look like a tacky explosion of buttons. Seeing a number of sites rolling their own share buttons with counts, for example The Next Web I decided to look into the various APIs on how to simply return the share count.

If you want to roll up all of these into a single jQuery plugin check out Sharrre

Many of these API calls and methods are undocumented, so anticipate that they will change in the future. Also, if you are planning on rolling these out across a site I would recommend creating a simple endpoint that periodically caches results from all of the APIs so that you are not overloading the services will requests.

Twitter

@webmechanicx
webmechanicx / canvas-upload.php
Created March 18, 2016 20:57 — forked from xjamundx/canvas-upload.php
php canvas base64 png decoder
<?php
// requires php5
define('UPLOAD_DIR', 'images/');
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
@webmechanicx
webmechanicx / getMineType.php
Created March 15, 2016 22:13
Get the Mine-Type of a File
<?php
/**
* get mime data of the file.
* @return {String} mime-type of the given file
* @param $filename String
*/
function get_mime($filename){
preg_match("/\.(.*?)$/", $filename, $m); # Get File extension for a better match
switch(strtolower($m[1])){
case "js": return "application/javascript";
@webmechanicx
webmechanicx / object2array.php
Created March 3, 2016 00:15
Simple Demonstration object to Array
<?php
$post_id = array((object) (array(
'index_id' => 'Mahfuz',
)), (object) (array(
'index_id' => '[email protected]',
)), (object) (array(
'index_id' => 142,
)));
foreach ($post_id as $key => $value) {
@webmechanicx
webmechanicx / sluggify.php
Created February 29, 2016 23:10
To covert a string to SEO friendly Slug-URL
<?php
/* Sluggify - Simple Logic to turn a string to slug url
* To covert a string to SEO friendly
*/
$title = "To covert a string to SEO friendly"
$slugs = preg_replace('/\%/',' percentage',$title);
$slugs = preg_replace('/\@/',' at ',$slugs);
@webmechanicx
webmechanicx / betweenTags.php
Created February 29, 2016 22:30
Simple PHP function to find anything between a tag
<?php
function betweenTags($string, $tagname)
{
$pattern = "#<\s*?$tagname\b[^>]*>(.*?)</$tagname\b[^>]*>#s";
preg_match($pattern, $string, $matches);
return $matches[1];
}
?>
@webmechanicx
webmechanicx / dom_tag_extractor.php
Created February 29, 2016 22:23
RegEx match open tags except XHTML self-contained tags
<?php
$selfClosing = explode(',', 'area,base,basefont,br,col,frame,hr,img,input,isindex,link,meta,param,embed');
$html = '
<p><a href="#">foo</a></p>
<hr/>
<br/>
<div>name</div>';
$dom = new DOMDocument();
@webmechanicx
webmechanicx / getInnerHTML.php
Created February 29, 2016 22:07
Get HTML,Text and Surrounded Tags
<?php
$html = <<< HTML
<body>
<h1>Hello,
<b>world!</b>
</h1>
</body>
HTML;
$dom = new DOMDocument;