Skip to content

Instantly share code, notes, and snippets.

View nagiyevelchin's full-sized avatar
🌴
On vacation

Elchin Nagiyev nagiyevelchin

🌴
On vacation
View GitHub Profile
@nagiyevelchin
nagiyevelchin / truncate.php
Last active February 12, 2022 16:00
Truncate a string to a certain length in PHP
<?php
/**
* Truncate a string to a certain length if necessary,
* optionally splitting in the middle of a word, and
* appending the $etc string or inserting $etc into the middle.
*
* @param string $string input string
* @param integer $length length of truncated text
* @param string $etc end string
* @param boolean $break_words truncate at word boundary
@nagiyevelchin
nagiyevelchin / subStringByLastNBSP.php
Last active October 7, 2023 11:34
This PHP function, subStringByLastNBSP, is designed to extract a substring from a given input string while ensuring that the resulting substring does not exceed a specified maximum length ($max). It also considers a minimum length ($min) that the substring should have before attempting to find the last non-breaking space character (' ') within t…
<?php
/**
* Extracts a substring from a given string, ensuring it does not exceed a maximum length,
* and ends at the last occurrence of a non-breaking space character (' ') within the specified range.
*
* @param string $string The input string to extract the substring from.
* @param int $max The maximum length of the substring.
* @param int $min The minimum length of the substring before considering the last non-breaking space.
* @param string $endstring Optional: The string to append to the extracted substring.
*
@nagiyevelchin
nagiyevelchin / validEmail.php
Last active February 12, 2022 15:56
Validate an email address in PHP
<?php
/**
* Validate an email address.
* Provide email address (raw input)
* Returns 1 if the email address has the email
* address format and the domain exists.
*/
function validEmail($email) {
$isValid = 1;
$atIndex = strrpos($email, "@");
@nagiyevelchin
nagiyevelchin / generatePassword.php
Last active February 12, 2022 15:56
Generate complex random password in PHP
<?php
/**
* Generate random password
* @param int $length [optional] [default 9] length password to be generated
* @return string
* @link http://www.webtoolkit.info/php-random-password-generator.html
* @since 12/1/12 11:30 AM
*/
function generatePassword($length = 9) {
$vowels = 'aeuy';
@nagiyevelchin
nagiyevelchin / deleteDirectory.php
Last active October 7, 2023 11:10
Efficient PHP Function for Deleting Directories and Their Contents
<?php
/**
* Efficient PHP Function for Deleting Directories and Their Contents
*
* This PHP function, named 'deleteDirectory,' is designed to safely and
* efficiently delete directories and all their contents. It first checks if
* the directory exists, and if not, it returns 'true' since the directory is
* already deleted. Next, it determines whether the specified path is a directory
* or a symbolic link and unlinks it if necessary.
@nagiyevelchin
nagiyevelchin / IPToLong.php
Last active February 12, 2022 15:55
Convert IP address to longint in PHP
<?php
/**
* IP to long
* @param type $ip
* @return int
*/
function ipToLong($ip = false) {
if (!$ip) {
$ip = getClientIPAddress();
}
@nagiyevelchin
nagiyevelchin / clientIPAddress.php
Last active February 12, 2022 15:54
Get clients IP address in PHP
<?php
/**
* Retuns client's IP address
* @return string
* @since 6/16/11 12:11 PM
*/
function getClientIPAddress() {
if (getenv("HTTP_CLIENT_IP")) {
$ip = getenv("HTTP_CLIENT_IP");
} else {
@nagiyevelchin
nagiyevelchin / detectMobileBrowsers.php
Last active February 12, 2022 15:54
Detect Mobile Browsers in PHP
<?php
/**
* Detect Mobile Browsers
* @link http://detectmobilebrowsers.com/
* @return boolean
*/
static function idMobileBrowser() {
$useragent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',
$useragent) || preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|
@nagiyevelchin
nagiyevelchin / httpCurlPost.php
Last active October 7, 2023 11:15
This PHP function is designed to send an HTTP POST request to a specified URL with given parameters, allowing you to customize headers and the referer. It uses the cURL library to handle the HTTP request and provides flexibility in customizing the request's headers and parameters. The function returns the response received from the POST request.
<?php
/**
* This function performs an HTTP POST request to a specified URL with given parameters.
*
* @param string $url The URL to which the POST request is sent.
* @param array $params An associative array containing the POST parameters as key-value pairs.
* @param string $referer The referer header for the request.
*
* @return mixed The response from the HTTP POST request.
*/
@nagiyevelchin
nagiyevelchin / checkVariableType.php
Last active February 12, 2022 15:52
Finds whether a variable is a needed type in PHP
<?php
/**
* Finds whether a variable is a unsigned float or a numeric string.
* Sometimes, we need to have no letters in the number and is_numeric does not quit the job.
* @param string The variable being evaluated.
* @link http://php.net/manual/en/function.is-numeric.php#47491
* @return 1|emptystring It returns 1 if okay and returns nothing "" if it's bad number formating
* @since 11/6/14 11:15 AM
*/
function isUnsignedFloat($val) {