JavaScript: Automatically make all the external links with rel=”nofollow noopener noreferrer”

JavaScript: Automatically make all the external links with rel=”nofollow noopener noreferrer”

The rel attribute specifies the relationship between the current document and the linked document. Search engines can use this attribute to get more information about a link.

rel Attribute Values

nofollowLinks to an unendorsed document, like a paid link.
(“nofollow” is used by Google, to specify that the Google search spider should not follow that link)
noreferrerRequires that the browser should not send an HTTP referer header if the user follows the hyperlink
noopenerRequires that any browsing context created by following the hyperlink must not have an opener browsing context

Add the following code into your HTML page between the <head> and </head> tags or before the closing </body> tag

<script>
    document.addEventListener('DOMContentLoaded', function () {
        var links = document.getElementsByTagName("a");
        var i;
        for (i = 0; i < links.length; i++) {
            if (location.hostname !== links[i].hostname) {
                links[i].rel = "nofollow noopener noreferrer";
                links[i].target = "_blank";
            }
        }
    });
</script>
PHP: Convert IPv4 to IPv6

PHP: Convert IPv4 to IPv6

The following PHP function will help you easily convert IPv4(IP address version 4) to IPv6(IP address version 6).

The function will return false if the provided IP address is invalid else it will return the equivalent IPv6

function convert_ipv4_to_ipv6($ip)
{
    if (!filter_var($ip, FILTER_VALIDATE_IP)) {
        return false;
    }

    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
        return $ip;
    }

    $bytes = array_map('dechex', explode('.', $ip));

    return vsprintf('0:0:0:0:0:ffff:%02s%02s:%02s%02s', $bytes);
}

PHP: Convert IPv4 to IPv6

Darken or lighten colors dynamically using PHP

Below is a snippet of the code and usage guide.

PHP Function

function colourBrightness($hex, $percent)
{
    // Work out if hash given
    $hash = '';
    if (stristr($hex, '#')) {
        $hex = str_replace('#', '', $hex);
        $hash = '#';
    }
    /// HEX TO RGB
    $rgb = [hexdec(substr($hex, 0, 2)), hexdec(substr($hex, 2, 2)), hexdec(substr($hex, 4, 2))];
    //// CALCULATE
    for ($i = 0; $i < 3; $i++) {
        // See if brighter or darker
        if ($percent > 0) {
            // Lighter
            $rgb[$i] = round($rgb[$i] * $percent) + round(255 * (1 - $percent));
        } else {
            // Darker
            $positivePercent = $percent - ($percent * 2);
            $rgb[$i] = round($rgb[$i] * (1 - $positivePercent)); // round($rgb[$i] * (1-$positivePercent));
        }
        // In case rounding up causes us to go to 256
        if ($rgb[$i] > 255) {
            $rgb[$i] = 255;
        }
    }
    //// RBG to Hex
    $hex = '';
    for ($i = 0; $i < 3; $i++) {
        // Convert the decimal digit to hex
        $hexDigit = dechex($rgb[$i]);
        // Add a leading zero if necessary
        if (strlen($hexDigit) == 1) {
            $hexDigit = "0" . $hexDigit;
        }
        // Append to the hex string
        $hex .= $hexDigit;
    }
    return $hash . $hex;
}

Function Usage

You do not have to give the color with the ‘#’ in front but if you do it will still return the new hex color with the ‘#’ auto-magically.

Lighten a color

$colour = '#ae64fe';
$brightness = 0.5; // 50% brighter
$newColour = colourBrightness($colour, $brightness);

Darken a color

$colour = '#ae64fe';
$brightness = -0.5; // 50% darker
$newColour = colourBrightness($colour, $brightness);
Magento – Get Current Store Details (ID, Code, Name and Status)

Magento – Get Current Store Details (ID, Code, Name and Status)

// Gets the current store’s details
$store = Mage::app()->getStore();

// Gets the current store’s id
$storeId = Mage::app()->getStore()->getStoreId();

// Gets the current store’s code
$storeCode = Mage::app()->getStore()->getCode();

// Gets the current website’s id
$websiteId = Mage::app()->getStore()->getWebsiteId();

// Gets the current store’s group id
$storeGroupId = Mage::app()->getStore()->getGroupId();

// Gets the current store’s name
$storeName = Mage::app()->getStore()->getName();

// Gets the current store’s sort order
$storeSortOrder = Mage::app()->getStore()->getSortOrder();

// Gets the current store’s status
$storeIsActive = Mage::app()->getStore()->getIsActive();

// Gets the current store’s locale
$storeLocaleCode = Mage::app()->getStore()->getLocaleCode();

// Gets the current store’s home url
$storeHomeUrl = Mage::app()->getStore()->getHomeUrl();
What are the differences between ‘git pull’ and ‘git fetch’?

What are the differences between ‘git pull’ and ‘git fetch’?

What are the differences between git pull and git fetch?

Answer:

In the simplest terms, git pull does a git fetch followed by a git merge.

You can do a git fetch at any time to update your remote-tracking branches under refs/remotes/<remote>/. This operation never changes any of your own local branches under refs/heads, and is safe to do without changing your working copy. I have even heard of people running git fetch periodically in a cron job in the background (although I wouldn’t recommend doing this).

A git pull is what you would do to bring a local branch up-to-date with its remote version, while also updating your other remote-tracking branches.

Git documentation: git pull

What are the differences between ‘git pull’ and ‘git fetch’?

When do you use git rebase instead of git merge?

When is it recommended to use git rebase vs. git merge?
Do I still need to merge after a successful rebase?

Answer:
Short Version

  • Merge takes all the changes in one branch and merge them into another branch in one commit.
  • Rebase says I want the point at which I branched to move to a new starting point

So when do you use either one?

Merge

Let’s say you have created a branch for the purpose of developing a single feature. When you want to bring those changes back to master, you probably want merge (you don’t care about maintaining all of the interim commits).

Rebase
A second scenario would be if you started doing some development and then another developer made an unrelated change. You probably want to pull and then rebase to base your changes from the current version from the repo.

How to reset AUTO_INCREMENT in MySQL and start counting from 1 again?

How to use MySQL to search in PHP serialized fields

Well, first rule – you should not do this. But if there is good reason, consider using such query for searching in index-based arrays:

SELECT * FROM table WHERE your_field_here REGEXP '.*;s:[0-9]+:"your_value_here".*'

In case you have assoc array serialized you can use:

SELECT * FROM table WHERE your_field_here REGEXP '.*"array_key_here";s:[0-9]+:"your_value_here".*'

Of course it won’t be very fast but in small tables should be enough

Source:
http://www.blastar.biz/2013/11/28/how-to-use-mysql-to-search-in-php-serialized-fields/

How to reset AUTO_INCREMENT in MySQL and start counting from 1 again?

MySQL: Select records from today, this week and this month

Assuming your date column is an actual MySQL date column:

SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 DAY) ORDER BY score DESC;        
SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 WEEK) ORDER BY score DESC;
SELECT * FROM jokes WHERE date > DATE_SUB(NOW(), INTERVAL 1 MONTH) ORDER BY score DESC;

Source: http://stackoverflow.com/a/5293226/1794834