DropIn was developed using javascript technology too. Please enable javascript to get the website running properly.
 
 


Your php snippets



 




watch_ifdebug [DEBUG]
2024-10-20 13:51:21
#title: watch_ifdebug
#desc: watch_ifdebug code time assessment
#tags: debug cron start stop watch
#cat: DEBUG


define('DEBUG', true);
define('PHP_BR', "<br>");
//define('DEBUG_WATCH_START', 0);
//define('DEBUG_WATCH_REPORT_ENTRY1', 0);
//...
//define('DEBUG_WATCH_REPORT_ENTRY15', 0);
//define('DEBUG_WATCH_END', 0);

/*
* Start the debug watch
*
* @param boolean $debug_var a debug value, if true the echo will be executed
* @return void
*/
function startwatch_ifdebug(bool $debug_var) {
if (!DEBUG || !$debug_var) {
return;
}
if (!defined("DEBUG_WATCH_START")) {
define('DEBUG_WATCH_START', hrtime(true));
}
}

/*
* Pause the debug watch
*
* @param boolean $debug_var a debug value, if true the echo will be executed
* @return void
*/
function pausewatch_ifdebug(bool $debug_var, bool $echo = false) {
if (!DEBUG || !$debug_var) {
return;
}
if (!defined("DEBUG_WATCH_START")) {
die("You need to start the watch!");
}
for($i=1;$i<=15;$i++) {
if (!defined("DEBUG_WATCH_REPORT_ENTRY".$i)) {
define("DEBUG_WATCH_REPORT_ENTRY".$i, round((hrtime(true) - DEBUG_WATCH_START)/1e+6,4));
break;
}
}

if ($echo) {
switch($i) {
case 1:
echo("First pause at: " . DEBUG_WATCH_REPORT_ENTRY1 . PHP_BR);
break;
case 2:
echo("Second pause at: " . DEBUG_WATCH_REPORT_ENTRY2 . PHP_BR);
break;
case 3:
echo("Third pause at: " . DEBUG_WATCH_REPORT_ENTRY3 . PHP_BR);
break;
case 4:
echo("Fourth pause at: " . DEBUG_WATCH_REPORT_ENTRY4 . PHP_BR);
break;
case 5:
echo("Fifth pause at: " . DEBUG_WATCH_REPORT_ENTRY5 . PHP_BR);
break;
case 6:
echo("Sixth pause at: " . DEBUG_WATCH_REPORT_ENTRY6 . PHP_BR);
break;
case 7:
echo("Seventh pause at: " . DEBUG_WATCH_REPORT_ENTRY7 . PHP_BR);
break;
case 8:
echo("Eighth pause at: " . DEBUG_WATCH_REPORT_ENTRY8 . PHP_BR);
break;
case 9:
echo("Nineth pause at: " . DEBUG_WATCH_REPORT_ENTRY9 . PHP_BR);
break;
case 10:
echo("Tenth pause at: " . DEBUG_WATCH_REPORT_ENTRY10 . PHP_BR);
break;
case 11:
echo("Eleventh pause at: " . DEBUG_WATCH_REPORT_ENTRY11 . PHP_BR);
break;
case 12:
echo("Twelveth pause at: " . DEBUG_WATCH_REPORT_ENTRY12 . PHP_BR);
break;
case 13:
echo("Thirteenth pause at: " . DEBUG_WATCH_REPORT_ENTRY13 . PHP_BR);
break;
case 14:
echo("Fourteenth pause at: " . DEBUG_WATCH_REPORT_ENTRY14 . PHP_BR);
break;
case 15:
echo("Fifteenth pause at: " . DEBUG_WATCH_REPORT_ENTRY15 . PHP_BR);
break;
}
}
}

/*
* Stop the debug watch
*
* @param boolean $debug_var a debug value, if true the echo will be executed
* @return void
*/
function stopwatch_ifdebug(bool $debug_var) {
if (!DEBUG || !$debug_var) {
return;
}
if (!defined("DEBUG_WATCH_START")) {
die("You need to start the watch first!");
}

for($i=1;$i<=15;$i++) {

if (defined("DEBUG_WATCH_REPORT_ENTRY".$i)) {

if ($i===1) {
echo("Watch final report:" . PHP_BR);
}

switch($i) {
case 1:
echo("First pause at: " . DEBUG_WATCH_REPORT_ENTRY1 . PHP_BR);
break;
case 2:
echo("Second pause at: " . DEBUG_WATCH_REPORT_ENTRY2 . PHP_BR);
break;
case 3:
echo("Third pause at: " . DEBUG_WATCH_REPORT_ENTRY3 . PHP_BR);
break;
case 4:
echo("Fourth pause at: " . DEBUG_WATCH_REPORT_ENTRY4 . PHP_BR);
break;
case 5:
echo("Fifth pause at: " . DEBUG_WATCH_REPORT_ENTRY5 . PHP_BR);
break;
case 6:
echo("Sixth pause at: " . DEBUG_WATCH_REPORT_ENTRY6 . PHP_BR);
break;
case 7:
echo("Seventh pause at: " . DEBUG_WATCH_REPORT_ENTRY7 . PHP_BR);
break;
case 8:
echo("Eighth pause at: " . DEBUG_WATCH_REPORT_ENTRY8 . PHP_BR);
break;
case 9:
echo("Nineth pause at: " . DEBUG_WATCH_REPORT_ENTRY9 . PHP_BR);
break;
case 10:
echo("Tenth pause at: " . DEBUG_WATCH_REPORT_ENTRY10 . PHP_BR);
break;
case 11:
echo("Eleventh pause at: " . DEBUG_WATCH_REPORT_ENTRY11 . PHP_BR);
break;
case 12:
echo("Twelveth pause at: " . DEBUG_WATCH_REPORT_ENTRY12 . PHP_BR);
break;
case 13:
echo("Thirteenth pause at: " . DEBUG_WATCH_REPORT_ENTRY13 . PHP_BR);
break;
case 14:
echo("Fourteenth pause at: " . DEBUG_WATCH_REPORT_ENTRY14 . PHP_BR);
break;
case 15:
echo("Fifteenth pause at: " . DEBUG_WATCH_REPORT_ENTRY15 . PHP_BR);
break;
}

} else {

break;
}
}

if (!defined("DEBUG_WATCH_END")) {
define("DEBUG_WATCH_END", round((hrtime(true) - DEBUG_WATCH_START)/1e+6,4));
}

echo("Final stop at: " . DEBUG_WATCH_END . PHP_BR);
}


//Usage
startwatch_ifdebug(true);

// Sleep for a while
usleep(100);
pausewatch_ifdebug(true);

// Sleep for an other while
usleep(100);

stopwatch_ifdebug(true);

var_dump_ifdebug [DEBUG]
2024-10-20 13:50:50
#title: var_dump_ifdebug
#desc: Replacement for var_dump with debug check
#tags: debug dump var_dump replacement
#cat: DEBUG


/**
* Replacement for var_dump with debug check
*
* @param boolean $debug_var a debug value, if true the var_dump will be executed
* @param list $args a var argument list
* @return void
* author 5mode.com
*/
function var_dump_ifdebug(bool $debug_var, ...$args): void
{
if (!DEBUG || !$debug_var) {
return;
}
foreach($args as $arg) {
var_dump($arg);
}
}
summary_ifdebug [DEBUG]
2024-10-20 13:50:22
#title: summary_ifdebug
#desc: Show some code summary
#tags: debug code summary
#cat: DEBUG


/*
* Show some code summary
*
* @param boolean $debug_var a debug value, if true the echo will be executed
* @return void
* @author 5mode.com
*/

define('DEBUG', true);
define('PHP_BR', "<br>");

function summary_ifdebug(bool $debug_var, string $scriptPath) {
if (!DEBUG || !$debug_var) {
return;
}
$a = file($scriptPath);
$linen = count($a);

$author="";
$authorKey = array_search_substr("* @author", $a, false, true);
if ($authorKey) {
$s = $a[$authorKey];
$author = ltrim($s, "* @author");
}

$copyrights="";
$crKey = array_search_substr("* @copyrights", $a, false, true);
if ($crKey) {
$s = $a[$crKey];
$copyrights = ltrim($s, "* @copyrights");
}

echo("Written code: $linen lines" . PHP_BR);
echo($author!==""?HTMLencode($author).PHP_BR:"");
echo($copyrights!==""?HTMLencode("Copyrights $copyrights").PHP_BR:"");
}
str_wordipos [STRING]
2024-10-20 13:49:53
#title: str_wordipos
#desc: Finds the position of a word in the given phrase
#tags: string word position
#cat: STRING


/**
* Finds the position of a word in the given phrase
*
* @param string $phrase the phrase being searched
* @param string $word the searched word
* @param int $offset the position to start the search from
* @return mixed the position of the word, otherwise false
* @author 5mode.com
*/
function str_wordipos(string $phrase, string $word, int $offset = 0)
{
if ($offset<0) {
$offset=0;
}
$word = strtolower($word);
$phrase = strtolower($phrase);
$aWords = explode(" ", $phrase);
$max = count($aWords) - 1;
$i = $offset;
while ($i <= $max) {
$word2 = $aWords[$i];
if ($word === $word2) {
return $i + 1;
}
$i++;
}
return false;
}
str_word_length [STRING]
2024-10-20 13:49:28
#title: str_word_length
#desc: Short words by the given length
#tags: short phrase words
#cat: STRING


/**
* Short words by the given length
*
* @param string $phrase the phrase being parsed
* @param int $length the word length to set
* @return string the resulting phrase
* @author 5mode.com
*/
function str_word_length(string $phrase, int $length): string
{
$aWords = explode(" ", $phrase);
foreach($aWords as &$word) {
$word = mb_substr($word, 0, $length);
}
return implode(" ", $aWords);
}
str_phrase_reverse [STRING]
2024-10-20 13:48:40
#title: str_phrase_reverse
#desc: Reverse the words in the given phrase
#tags: string phrase reverse
#cat: STRING


/**
* Reverse the words in the given phrase
*
* @param string $string the phrase to reverse
* @return string the resulting reversed phrase
* @author 5mode.com
*/
function str_phrase_reverse(string $string): string
{
settype($aWords, "array");
$aWords = explode(" ", $string);
$aWords = array_reverse($aWords);
return implode(" ", $aWords);
}
rightWord [STRING]
2024-10-20 13:48:15
#title: rightWord
#desc: Return the right word of the given phrase
#tags: string phrase sentence
#cat: STRING


/**
* Return the right word of the given phrase
*
* @param string $phrase the phase being processed
* @return string the first word
* @auhtor 5mode.com
*/
function rightWord(?string $phrase): string
{
if (!isset($phrase) || $phrase==="") {
return "";
}

$aWords = explode(" ", $phrase);
return $aWords[count($aWords)-1];
}
right [STRING]
2024-10-20 13:47:27
#title: right
#desc: Right cut the given string
#tags: right string
#cat: STRING


/**
* Right cut the given string
*
* @param string $string the string being cut on the right
* @param int $length the length of the substring to return
* @return string the resulting substring
* @author 5mode.com
*/
function right(?string $string, int $length): string
{
if (!isset($string) || $string === "") {
return "";
}
return mb_substr($string, mb_strlen($string) - $length);
}
leftWord [STRING]
2024-10-20 13:46:58
#title: leftWord
#desc: Return the left word of the given phrase
#tags: string phrase sentence
#cat: STRING


**
* Return the left word of the given phrase
*
* @param string $phrase the phase being processed
* @return string the first word
* @author 5mode.com
*/
function leftWord(?string $phrase): string
{
if (!isset($phrase) || $phrase==="") {
return "";
}

$aWords = explode(" ", $phrase);
return $aWords[0];
}
left [STRING]
2024-10-20 13:46:34
#title: left
#desc: Left cut the given string
#tags: string left cut
#cat: STRING


/**
* Left cut the given string
*
* @param string $string the string being cut on the left
* @param int $length the length of the substring to return
* @return string the resulting substring
* @author 5mode.com
*/
function left(?string $string, int $length): string
{
if (!isset($string) || $string === "") {
return "";
}
return mb_substr($string, 0, $length);
}
isYoutubeUrl [VALIDATION]
2024-10-20 13:45:21
#title: isYoutubeUrl
#desc: Check if the given url is a valid Youtube url
#tags: youtube url string
#cat: VALIDATION


/**
* Check if the given url is a valid Youtube url
*
* Eg:
* http://youtube.com/watch?v=DRrwr24334
*
* @param string $url the url to check
* @return bool if the url is Youtube url, true/false
* @author 5mode.com
*/
function isYoutubeUrl(string $url): bool
{
$url= strtolower($url);
$pattern1 = "/^http(s)?\:\/\/(www\.)?youtube\.com\/watch\?v\=[\w]+$/";
if (preg_match($pattern1, $url)) {
$retval=true;
} else {
$retval=false;
}
return $retval;
}
isUrl [VALIDATION]
2024-10-20 13:45:08
#title: isUrl
#desc: Check if the given url is a valid domain url
#tags: net url validation
#cat: VALIDATION


/**
* Check if the given url is a valid domain url
*
* @param string $url the url to check
* @return bool if the url is a valid domain url, true/false
* author 5mode.com
*/
function isUrl(string $url): bool
{
$url= strtolower($url);
$pattern1 = "/^http(s)?\:\/\/(www\.)?\w+\.\w+$/";
$pattern2 = "/^http(s)?\:\/\/(www\.)?\w+\.\w+.\w+$/";
if (preg_match($pattern1, $url) || preg_match($pattern2, $url)) {
$retval=true;
} else {
$retval=false;
}
return $retval;
}
isSubdomainHost [NET]
2024-10-20 13:44:44
#title: isSubdomainHost
#desc: Determine if the given host is a subdomain
#tags: net internet subdomain check
#cat: NET


/**
* Determine if the given host is a subdomain
*
* @param string $subdomain passed by ref, return the subdomain
* @return bool if the domain is a subdomain, true/false
* @author 5mode.com
*/
function isSubdomainHost(string &$subdomain) {
$hostname = str_replace("www.", "", strtolower($_SERVER['HTTP_HOST']));
$ipos = mb_stripos($hostname, ".");
$subdomain = left($hostname, $ipos);
$isSubdomain = true;
if ($subdomain==="5mode") {
$isSubdomain = false;
}
return $isSubdomain;
}
isLatinLang [VALIDATION]
2024-10-20 13:44:18
#title: isLatinLang
#desc: Test if a word is of a latin language
#tags: string validation latin western language
#cat: VALIDATION


**
* Test if a word is of a latin language
*
* @param string$word the word to test
* @return boolif $word is of a latin language, true/false
* @author 5mode.com
*/
function isLatinLang(string $word): bool
{

$isNonLatinLang = preg_match("/^[\w-]+[\x{31C0}-\x{31EF}\x{3300}-\x{33FF}\x{3400}-\x{4DBF}\x{4E00}-\x{9FFF}\x{F900}-\x{FAFF}\x{FE30}-\x{FE4F}]+$/u", $word) ||
preg_match("/^[\x{31C0}-\x{31EF}\x{3300}-\x{33FF}\x{3400}-\x{4DBF}\x{4E00}-\x{9FFF}\x{F900}-\x{FAFF}\x{FE30}-\x{FE4F}]+$/u", $word) ||
preg_match("/^[\x{31C0}-\x{31EF}\x{3300}-\x{33FF}\x{3400}-\x{4DBF}\x{4E00}-\x{9FFF}\x{F900}-\x{FAFF}\x{FE30}-\x{FE4F}]+[\w-]+$/u", $word) ||
preg_match("/^[\w-]+[\x{31C0}-\x{31EF}\x{3300}-\x{33FF}\x{3400}-\x{4DBF}\x{4E00}-\x{9FFF}\x{F900}-\x{FAFF}\x{FE30}-\x{FE4F}]+[\w-]+$/u", $word);

return !$isNonLatinLang;
}
grabHostingIPs [NET]
2024-10-20 13:43:49
#title: grabHostingIPs
#desc: Retrieve the hosting ip for the given domain
#tags: hosting ip DNS_A net network internet
#cat: NET


/**
* Retrieve the hosting ip for the given domain
*
* @param string $targetDomain the domain to check
* @return array the related DNS_A records
* @author 5mode.com
*/
function grabHostingIPs(string $targetDomain)
{
$ret = [];

$a = dns_get_record($targetDomain, DNS_A);
if ($a === false) {
} else
$ret = $a;
}
return $ret;
}
grabHeaders [NET]
2024-10-20 13:42:16
#title: grabHeaders
#desc: Retrieve headers of a website
#tags: server response headers net network internet
#cat: NET


/**
* Retrieve headers of a website
*
* @param string $targetDomain the domain to check
* @return array the related response headers
* @author 5mode.com
*/
function grabHeaders(string $targetDomain)
{
$ret = [];

$a = get_headers("http://" . $targetDomain);
if ($a === false) {
} else
$ret = $a;
}
return $ret;
}
getResource [INT]
2024-10-20 13:41:58
#title: getResource
#desc: Get resources translated
#tags: resource translated int
#cat: INT


/**
* Get resources translated
*
* @param string $resource the string to get translated
* @param string $destLocale the destination locale
* @param string $localeKey the locale key
* @return string the translated resource
* @author 5mode.com
*/
$LOCALE = [

'it-IT' => [
'/index.php' => [
'ping' => "pong",
]
],
'en-US' => [
'/index.php' => [
'ping' => "pong",
]
]
]

function getResource(string $resource, string $destLocale = "en-US", string $localeKey = "/".SCRIPT_FILENAME): string
{

global $LOCALE;

if ($destLocale === "en-US") {
return $resource;
}

if (($LOCALE[$destLocale][$localeKey]['ping']??"")=="") {
return $resource;
}

if (($LOCALE[$destLocale][$localeKey][$resource]??"")=="") {
return $string;
}

return $LOCALE[$destLocale][$localeKey][$resource];
}
enableLinks [STRING]
2024-10-20 13:41:28
#title: enableLinks
#desc: Enable links in the given text
#tags: string links transformation
#cat: STRING


/**
* Enable links in the given text
*
* @param string $text the text being parsed for links
* @return string the text with links enabled
* @author 5mode.com
*/
function enableLinks(string $text): string
{
return preg_replace("/(https?:\/\/)([\da-z\.-]+)\.([a-z\.]{2,8})(\/?[\da-zA-Z\-\?\/\&\#\=]+)?/i", "<a href='\\0' target=\"_blank\">\\0</a>", $text);
}
enableEmoticons [STRING]
2024-10-20 13:40:57
#title: enableEmoticons
#desc: Enable emoticons in the given text
#tags: string emoticons transformation
#cat: STRING


/**
* Enable emoticons in the given text
*
* @param string $text the text being parsed for emoticons
* @return string the text with the emoticons enabled
* @author 5mode.com
*/
function enableEmoticons(string $text): string
{
$emot = [
'[:-)]' => "&#128578;",
'[:-p]' => "&#129297;",
'[:c)]' => "&#128512;",
'[;-)]' => "&#128521;",
'[:-J]' => "&#128527;",
'[;-P]' => "&#128540;",
'[:-/]' => "&#128533;",
'[@-)]' => "&#128531;",
'[B-)]' => "&#128526;",
'[=]]' => '&#128515;',
'[=)]' => "&#128516;",
'[*-)]' => "&#128525;"
];

return str_ireplace(array_keys($emot),array_values($emot), $text);
}
enableEmails [STRING]
2024-10-20 13:40:01
#title: enableEmails
#desc: Enable the email address in the given text
#tags: string email transformation
#cat: STRING


/**
* Enable the email address of the given text
*
* @param string $text the text being parsed for emails
* @return string the text with email enabled
* @author 5mode.com
*/
function enableEmails(string $text, bool $masked = false): string
{
$callable_masked = function($aResults) {
$result = implode("", $aResults);
return "<a href='mailto:$result'>" . mb_strrichr($result, "@", true) . "@.." . mb_strrichr($result, ".", false) . "</a>";
};

$callable_unmasked = function($aResults) {
$result = implode("", $aResults);
return "<a href='mailto:$result'>$result</a>";
};

$regexPattern = "/(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}/i";

if ($masked) {
$callable = $callable_masked;
} else {
$callable = $callable_unmasked;
}

return preg_replace_callback($regexPattern, $callable, $text);
}
echo_ifdebug [DEBUG]
2024-10-20 13:39:15
#title: echo_ifdebug
#desc: Replacement for echo with debug check
#tags: debug echo replacement
#cat: DEBUG


**
* Replacement for echo with debug check
*
* @param boolean $debug_var a debug value, if true the echo will be executed
* @param list $args a var argument list
* @return void
* @author 5mode.com
*/
function echo_ifdebug(bool $debug_var, ...$args): void
{
if (!DEBUG || !$debug_var) {
return;
}
foreach($args as $arg) {
echo $arg;
}
}
array_search_substr [ARRAY]
2024-10-20 13:37:38
#title: array_search_substr
#desc: Searches the array for a given value substring
#tags: string array search substring
#cat: ARRAY


/**
* Searches the array for a given substring and returns the corresponding key if successful otherwise null
*
* @param mixed $needle the string to search for
* @param array $array the array being searched
* @param bool $case if the search should happen in a case sensitive fashion, true/false
* @param bool $atStart if the string should appear at the beginning of the value, true/false
* @return mixed the key containing the string, otherwise null
* @author 5mode.com
*/
function array_search_substr(string $needle, &$array, bool $case = false, bool $atStart = false)
{
$retval = null;
if (!$case) {
$needle = strtolower($needle);
}
foreach ($array as $key => $value) {

if (!$case) {
$value = strtolower($value);
}

$_res = false;
if ($atStart) {
$value = trim($value);
if (left($value, strlen($needle)) === $needle) {
$_res = true;
}
} else {
if (mb_stripos($value, $needle) === false) {
} else {
$_res = true;
}
}

if ($_res) {
$retval = $key;
break;
}
}
return $retval;
}
HTMLencode [STRING]
2024-10-20 13:33:24
#title: HTMLencode
#desc: Encode any HTML entity in the given string
#tags: string html encode transformation
#cat: STRING


/**
* Encode any HTML entity in the given string
*
* @param string $s the string to encode
* @param bool $withBR keep the BR tag, true/false
* @return string the string encoded
* @author 5mode.com
*/
public static function HTMLencode(?string $s, bool $withBR = false): string
{
if (!isset($s)) {
return "";
}

$s = str_ireplace("'", "'", $s);
$s = str_ireplace(""", "\"", $s);
$s = str_ireplace("\", chr(10), $s);
$s = htmlspecialchars($s, ENT_QUOTES |ENT_IGNORE | ENT_HTML5, "UTF-8");

if ($withBR) {
$s = str_ireplace(chr(10), "<br>", $s);
}

return $s;
}