Quick Pastes

This is quick copy and paste snippets, I use all the time:

PHP. Report All PHP Errors

error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);

Composer. As Lean and Minimal  As Possible

composer update --prefer-dist -vvv --profile

PHP. XML to Array

function xmlToArray($xml) {
    if (is_string($xml)) {
        $sml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
    }

    $array = [];
    $array['TagName'] = $xml->getName();
    $array['TagText'] = trim((string) $xml);
    $array['TagAttributes'] = array();
    $array['TagChildren'] = array();

    foreach ($xml->attributes() as $k => $v) {
        $array['TagAttributes'][$k] = (string) $v;
    }

    foreach ($xml->children() as $k => $v) {
        $array['TagChildren'][] = xmlToArray($v);
    }

    return $array;
}

Get Content Between Two X/HTML Tags

function getTagsContent($string, $tagname) {
    $pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
    preg_match($pattern, $string, $matches);
    return $matches[1];
}

PHP. Is it a POST request?

$isPost = $_SERVER['REQUEST_METHOD'] == "POST" ? true : false;

 

Linux. Creating Swap File

free -m
/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024 // 1GB, or
/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=2048 // 2GB
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1

# Now add an entry to your /etc/fstab file to persist the change

/var/swap.1 none swap sw 0 0

 

JavaScript. Find Number of Days in Date Range

function numberOfDaysInDaterange(date1, date2) {
        var _MS_PER_DAY = 1000 * 60 * 60 * 24;
        var utc1 = Date.UTC(date1.getFullYear(), date1.getMonth(), date1.getDate());
        var utc2 = Date.UTC(date2.getFullYear(), date2.getMonth(), date2.getDate());

        return Math.floor((utc2 - utc1) / _MS_PER_DAY);

}

JavaScript. Limit User Entered Text to Numeric Values Only

$(function () {
    $('form[name=FORM_.EDIT] input[name=Price]').keyup(function () {
        var numeric = $.trim($('form[name=FORM_.EDIT] input[name=Price]').val());

        numeric = numeric.replace(/[^0-9.]/g, '');
        $('form[name=FORM_.EDIT] input[name=Price]').val(numeric);
    });
});

PHP. Find Number of Days in Date Range

function numberOfDaysInDaterange($time_start, $time_end) {
        $time_start = is_numeric($time_start) ? $time_start : strtotime($time_start);
        $time_end = is_numeric($time_end) ? $time_end : strtotime($time_end);
        $time_start = mktime(0, 0, 0, date('m', $time_start), date('d', $time_start), date('Y', $time_start));
        $time_end = mktime(0, 0, 0, date('m', $time_end), date('d', $time_end), date('Y', $time_end));
        $range = array();
        if ($time_start <= $time_end) {
            $range[] = date('Y-m-d', $time_start);
            while ($time_start < $time_end) {
                $time_start += 86400; // add 24 hours
                $range[] = date('Y-m-d', $time_start);
            }
        }
        return $range;
}

JavaScript. remove checkboxes being arrays in WP

function formatInput (selector) {
    var array = $(selector + ' :input').serializeArray();
    var json = {};

    jQuery.each(array, function () {
        var name = $.trim(this.name.split('[').join('').split(']').join().split(',').join('')); // Remove checkboxes being arrays in the WP plugin
        json[name] = this.value || '';
    });


    return json;
}

 

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form. Links to this article are welcome

Other Related Articles