Skip to content

Instantly share code, notes, and snippets.

@iprodev
iprodev / has-duplicate.php
Last active December 14, 2018 18:00
PHP : Check if array has duplicate values
<?php
/**
* Check if array has duplicate values
*
* @param array $array Specific array to check
* @return boolean
*/
function array_has_duplicate( $array ) {
return count( $array ) !== count( array_keys( array_flip( $array ) ) );
@iprodev
iprodev / faster-mysql-counting-on-joins.sql
Last active December 14, 2018 18:09
SQL : Faster MySQL Counting on JOINS
SELECT
user_accounts.id,
user_accounts.company_name,
IFNULL(suppliers.count, 0) AS suppliers,
IFNULL(customers.count, 0) AS customers,
IFNULL(inventory_transactions.count, 0) AS transactions
FROM user_accounts
LEFT JOIN (SELECT
user_account_id,
@iprodev
iprodev / search-and-replace.sql
Created December 14, 2018 18:11
SQL : Search and Replace
UPDATE tableName
SET columnName = REPLACE(
columnName,
'TEXT TO SEARCH FOR',
'TEXT TO REPLACE WITH'
)
@iprodev
iprodev / openssl_hash_cipher.php
Last active December 14, 2018 19:43
PHP : Powerful encrypt and decrypt using OpenSSL
<?php
/**
* Powerful method to encrypt or decrypt a plain text string
* initialization vector(IV) has to be the same when encrypting and decrypting
*
* @param string $action: can be 'encrypt' or 'decrypt'
* @param string $string: string to encrypt or decrypt
*
* @return string
*/
@iprodev
iprodev / neon.css
Last active December 14, 2018 18:37
CSS : Neon Text Effect with CSS3
body {
background-color:#424242;
}
.neon-effect {
text-align:center;
font-size:40px;
margin:20px 0 20px 0;
color:#ffffff;
text-shadow: 0 0 10px #ffffff,
@iprodev
iprodev / make_api_calls.php
Created December 14, 2018 18:46
PHP : Make API Calls in PHP
<?php
/**
* Make API Calls in PHP
*
* @param string $method The HTTP method (GET or POST) to be used
* @param string $url The URL
* @param array $data The data should send with POST
* @return string Response from the url
*/
function CallAPI( $method, $url, $data = false ) {
@iprodev
iprodev / get-date-diff.php
Created December 14, 2018 19:19
PHP : Get human readable time difference between 2 dates
<?php
/**
* Get human readable time difference between 2 dates
*
* Return difference between 2 dates in year, month, hour, minute or second
* The $precision caps the number of time units used: for instance if
* $time1 - $time2 = 3 days, 4 hours, 12 minutes, 5 seconds
* - with precision = 1 : 3 days
* - with precision = 2 : 3 days, 4 hours
* - with precision = 3 : 3 days, 4 hours, 12 minutes
@iprodev
iprodev / curl.php
Created December 14, 2018 19:32
PHP : Limit CURL so it doesn't download huge files
<?php
# with a callback
$sizeLimit = 100000;
curl_setopt($ch, CURL_PROGRESSFUNCTION, function ($ch, $totalBytes, $receivedBytes) use ($sizeLimit) {
if ($totalBytes > $sizeLimit) {
return 1; // return non-zero value to abort transfer
}
});
# And check the error (using curl_errno()) for CURLE_ABORTED_BY_CALLBACK, if i want to know the reason is size limit.
# source: https://www.reddit.com/r/PHP/comments/641uud/is_there_any_easy_way_to_limit_curl_via_php_so_it/dg12n30/
@iprodev
iprodev / GPLv3.md
Created December 16, 2018 02:58 — forked from kn9ts/GPLv3.md
GPLv3 explained

GPL3 LICENSE SYNOPSIS

TL;DR* Here's what the license entails:

1. Anyone can copy, modify and distribute this software.
2. You have to include the license and copyright notice with each and every distribution.
3. You can use this software privately.
4. You can use this software for commercial purposes.
5. If you dare build your business solely from this code, you risk open-sourcing the whole code base.
@iprodev
iprodev / file_get_contents_utf.php
Last active December 29, 2018 11:12
PHP : Reads entire file into a string without UTF8/UTF16 bom
<?php
/**
* Reads entire file into a string without UTF8/UTF16 bom
*
* @param string $file Path of the file to read.
* @return string The read data or FALSE on failure.
*/
function file_get_contents_utf( $file = null ) {
if ( is_readable( $file ) ) {
if ( !( $fh = fopen( $file, 'r' ) ) ) return false;