Для получения полного доступа
зарегистрируйтесь.

Section:
Sub-section::
Languages:


Gravatar image
jimkubatai
  • Reputation: 1
  • Snippets: 1
  • Revisions: 0

A customizable bool-lever, after the call, the value inverts its value.

    public struct BoolLever
    {
        private bool _value;
        public bool Value
        {
            get
            {
                bool temp = _value;

                if (_value)
                {
                    if (InvertTrue)
                    {
                        _value = false;
                    }
                }
                else
                {
                    if (InvertFalse)
                    {
                        _value = true;
                    }
                }

                return temp;
            }
        }

        public bool InvertTrue { get; private set; }
        public bool InvertFalse { get; private set; }

        public BoolLever(bool value, bool invertTrue, bool invertFalse)
        {
            InvertTrue = invertTrue;
            InvertFalse = invertFalse;
            _value = value;
        }

    }
Gravatar image
tetraset
  • Reputation: 6
  • Snippets: 3
  • Revisions: 0
function chunkedCopy($from, $to) {
    # 1 meg at a time, you can adjust this.
    $buffer_size = 1048576;
    $ret = 0;
    $fin = fopen($from, "rb");
    $fout = fopen($to, "w");
    while(!feof($fin)) {
    	$ret += fwrite($fout, fread($fin, $buffer_size));
    }
    fclose($fin);
    fclose($fout);
    return $ret;
}
SilverFire
  • Reputation: 3
  • Snippets: 1
  • Revisions: 0

The function above checks, whether the current controller's action is the same as defined in the defaultRoute property of application.

function isHomePage() {
  $actualRoute = Yii::$app->controller->getRoute();

  list($controller, $actionId) = Yii::$app->createController('');
  $actionId = !empty($actionId) ? $actionId : $controller->defaultAction;
  $defaultRoute = $controller->getUniqueId() . '/' . $actionId;

  return $actualRoute === $defaultRoute;
}
Kosuha606
  • Reputation: 11
  • Snippets: 11
  • Revisions: 0

Впринципе ничего особенного, просто сетка на 12 колонок, привязанная к data параметрам

[data-rbk-container] { width: 1170px; margin: 0 auto; }
[data-rbk-row] { display: block; }
[data-rbk-row]:after, [data-rbk-row]:before { content: ' '; display: block; clear: both; height: 0; }
[data-rbk-col] { display: block; box-sizing: border-box; width: 50%; padding: 15px; float: left; }
[data-rbk-col="1"] { width: 8.333%; }
[data-rbk-col="2"] { width: 16.666%;}
[data-rbk-col="10"] { width: 83.333%;}
[data-rbk-col="3"] { width: 25%; }
[data-rbk-col="2.5"] { width: 20%; } /* for 5th colons */
[data-rbk-col="9"] { width: 75%; }
[data-rbk-col="4"] { width: 33.333%; }
[data-rbk-col="8"] { width: 66.666%; }
[data-rbk-col="5"] { width: 41.666%; }
[data-rbk-col="7"] { width: 58.333%; }
[data-rbk-col="11"] { width: 91.666%; }
[data-rbk-col="12"] { width: 100%; }

View Demo

Gravatar image
blacksmoke26
  • Reputation: 13
  • Snippets: 11
  • Revisions: 0
/**
 * Split the SQL dump code and return as queries array.
 * @param string $raw SQL code to parse
 * @return array List of queries
 */
function splitSqlText ( $raw ) {
	// the regex needs a trailing semicolon
	$raw = trim ( (string) $raw );

	if ( substr ( $raw, -1 ) !== ";") {
		$raw .= ";";
	}

	// i spent 3 days figuring out this line
	preg_match_all( "/(?>[^;']|(''|(?>'([^']|\\')*[^\\\]')".
		"))+;/ixU", $raw, $matches, PREG_SET_ORDER );

	$querySplit = [];

	foreach ( $matches as $match ) {
		// get rid of the trailing semicolon
		$querySplit[] = substr( $match[0], 0, -1 );
	}

	return $querySplit;
}
Gravatar image
blacksmoke26
  • Reputation: 13
  • Snippets: 11
  • Revisions: 0
/**
 * Validate MySQL Timestamp
 * @param string $value Value to check (Y-m-d h:i:s)
 * @return bool TRUE on valid | FALSE anyway
 */
function isTimestampValid ( $value ) {
	$date = array();
	if ( !preg_match ('/^(?<y>19\d\d|20\d\d)\-(?<m>0[1-9]|1[0-2])\-' .
		'(?<d>0\d|[1-2]\d|3[0-1]) (?<h>0\d|1\d|2[0-3]' .
		')\:(?<i>[0-5][0-9])\:(?<s>[0-5][0-9])$/', $value, $date) ) {
		return false;
	}

	return checkdate ( $date['m'], $date['d'], $date['y'] );
}

/************
 * EXAMPLE
 ***********/
var_dump ( isTimestampValid ('2016-09-10 23:21:09') );

/***********
 * OUTPUT
 **********/
# (bool) true
Gravatar image
blacksmoke26
  • Reputation: 13
  • Snippets: 11
  • Revisions: 0
/**
 * Generate mime types list using apache svn defination
 * @link http://php.net/manual/en/function.mime-content-type.php#107798
 * @return array [key=>value] pairs of mime types (extension=>mime_type)
 */
function generateMimeTypes () {
	$list = [];
	$url = 'http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types';
    
	foreach ( @explode ( "\n", @file_get_contents ($url) ) as $x ) {
		if ( isset( $x[0] )
			&& $x[0] !== '#'
			&& preg_match_all ( '#([^\s]+)#', $x, $out )
			&& isset( $out[1] )
			&& ( $c = count ( $out[1] ) ) > 1 ) {
			for ( $i = 1; $i < $c; $i++ ) {
				$key = (string) $out[1][$i];
				$list[$key] = $out[1][0];
			}
		}
	}

	return @ksort($list, \SORT_STRING )
		? $list
		: [];
}

/************
 * EXAMPLE
 ***********/
print_r ( generateMimeTypes () );

/***********
 * OUTPUT
 **********/
 [
    '3dml' => 'text/vnd.in3d.3dml'
    '3ds' => 'image/x-3ds'
    '3g2' => 'video/3gpp2'
    '3gp' => 'video/3gpp'
    '7z' => 'application/x-7z-compressed'
    'aab' => 'application/x-authorware-bin'
    'aac' => 'audio/x-aac'
    'aam' => 'application/x-authorware-map'
    'aas' => 'application/x-authorware-seg'
    'abw' => 'application/x-abiword'
    'ac' => 'application/pkix-attr-cert'
    'acc' => 'application/vnd.americandynamics.acc'
    'ace' => 'application/x-ace-compressed'
    'acu' => 'application/vnd.acucobol'
    'acutc' => 'application/vnd.acucorp'
    'adp' => 'audio/adpcm'
    'aep' => 'application/vnd.audiograph'
    'afm' => 'application/x-font-type1'
    'afp' => 'application/vnd.ibm.modcap'
    'ahead' => 'application/vnd.ahead.space'
    'ai' => 'application/postscript'
    'aif' => 'audio/x-aiff',
    ...
]
Gravatar image
blacksmoke26
  • Reputation: 13
  • Snippets: 11
  • Revisions: 0
/**
 * Divide an array into a desired number of split lists
 * @link http://www.php.net/manual/en/function.array-chunk.php#75022
 * @param array $list The Array
 * @param int $size Partition Size
 * @return array The partitioned array
 */
function columnsPartition ( array $list, $size ) {
	$listLen = count ( $list );

	if ( !$listLen || $size < 1 ) {
		return [];
	}

	$partLen = floor ( $listLen / $size );
	$partRem = $listLen % $size;
	$partition = [];
	$mark = 0;

	for ( $px = 0; $px < $size; $px++ ) {
		$increment = ( $px < $partRem ) ? $partLen + 1 : $partLen;

		$partition[ $px ] = array_slice ( $list, $mark, $increment );
		$mark += $increment;
	}

	return $partition;
}

/************
 * EXAMPLE
 ***********/
var_dump ( columnsPartition(range('A','I'), 3) );

/***********
 * OUTPUT
 **********/
array (size=3)
  0 => 
    array (size=3)
      0 => string 'A' (length=1)
      1 => string 'B' (length=1)
      2 => string 'C' (length=1)
  1 => 
    array (size=3)
      0 => string 'D' (length=1)
      1 => string 'E' (length=1)
      2 => string 'F' (length=1)
  2 => 
    array (size=3)
      0 => string 'G' (length=1)
      1 => string 'H' (length=1)
      2 => string 'I' (length=1)
Gravatar image
blacksmoke26
  • Reputation: 13
  • Snippets: 11
  • Revisions: 0
/**
 * Strip text longer then given length
 * @param string $text Text to be stripped
 * @param int $length Length of chars to keep
 * @param string $glueChar (optional) Show when length exceeded
 * @return string|null Stripped text | Text was empty
 */
function getCharsByLength ( $text, $length, $glueChar = '...' ) {
	if ( !trim ($text ) ) {
		return null;
	}

	/** @var int $actualLength */
	$actualLength = function_exists('mb_strlen')
		? mb_strlen($text)
		: strlen($text);

	// desired length is same or less, return as it is
	if ( $actualLength <= $length ) {
		return $text;
	}

	return function_exists ( 'mb_substr' )
		? mb_substr ($text, 0, $length, 'utf8' ) . $glueChar
		: substr ($text, 0, $length ) . $glueChar;
}

/************
 * EXAMPLE
 ***********/
getCharsByLength ('Νο εαμ σθμμο vολθτπατ. Εα εστ ομνεσ ιμπερδιετ εφφιcιαντθρ', 10);

/***********
 * OUTPUT
 **********/
# Νο εαμ σθμ...
IStranger
  • Reputation: 17
  • Snippets: 8
  • Revisions: 0

High-performance function checks whether specified array contains only integer values (or integer-numeric strings):


/**
 * Checks whether all elements of an array are integers (integer value or string with integer value).
 * Note: this is very efficient method can be used for array with 10000+ items.
 *
 * @param int[]|string[] $array One dimensional array to check
 *
 * @return bool
 */ 
function hasOnlyIntEntries(array $array)
{
    return ctype_digit(join('', $array));
}