Skip to content

Instantly share code, notes, and snippets.

View r37r0m0d3l's full-sized avatar

Anton Trofymenko r37r0m0d3l

View GitHub Profile
@r37r0m0d3l
r37r0m0d3l / array_dereferencing.php
Created September 12, 2012 10:38
Array dereferencing in php < 5.4
<?php
function ad($array, $index)
{
return array_key_exists($index, $array) ? $array[$index] : null;
}
@r37r0m0d3l
r37r0m0d3l / url_variable.php
Created September 12, 2012 10:40
Url variable encoding/decoding
<?php
class url_var
{
/**
* encode variable for url
* @param $var
* @return string
*/
static function enc($var)
{
@r37r0m0d3l
r37r0m0d3l / str_implode_wrapped.php
Created September 12, 2012 10:42
String implode wrapped
<?php
function str_implode_wrapped($before, $after, $array, $glue = '')
{
return $before.implode($after.$glue.$before, $array).$after;
}
@r37r0m0d3l
r37r0m0d3l / translit.php
Created September 12, 2012 10:43
Transliteration function
<?php
function translit($cyr_str)
{
return strtr($cyr_str, array(
'Ґ'=> 'G', 'Ё'=> 'Yo', 'Є'=> 'E', 'Ї'=> 'Yi', 'І'=> 'I', 'і'=> 'i', 'ґ'=> 'g', 'ё'=> 'yo', '№'=> '#', 'є'=> 'e',
'ї'=> 'yi', 'А'=> 'A', 'Б'=> 'B', 'В'=> 'V', 'Г'=> 'G', 'Д'=> 'D', 'Е'=> 'E', 'Ж'=> 'Zh', 'З'=> 'Z', 'И'=> 'I',
'Й'=> 'Y', 'К'=> 'K', 'Л'=> 'L', 'М'=> 'M', 'Н'=> 'N', 'О'=> 'O', 'П'=> 'P', 'Р'=> 'R', 'С'=> 'S', 'Т'=> 'T',
'У'=> 'U', 'Ф'=> 'F', 'Х'=> 'H', 'Ц'=> 'Ts', 'Ч'=> 'Ch', 'Ш'=> 'Sh', 'Щ'=> 'Sch', 'Ъ'=> '`', 'Ы'=> 'Yi', 'Ь'=> '',
'Э'=> 'E', 'Ю'=> 'Yu', 'Я'=> 'Ya', 'а'=> 'a', 'б'=> 'b', 'в'=> 'v', 'г'=> 'g', 'д'=> 'd', 'е'=> 'e', 'ж'=> 'zh',
'з'=> 'z', 'и'=> 'i', 'й'=> 'y', 'к'=> 'k', 'л'=> 'l', 'м'=> 'm', 'н'=> 'n', 'о'=> 'o', 'п'=> 'p', 'р'=> 'r',
@r37r0m0d3l
r37r0m0d3l / str_is_email.php
Created September 12, 2012 12:13
String is email
<?php
function str_is_email($email)
{
return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
}
@r37r0m0d3l
r37r0m0d3l / str_is_match.php
Created September 12, 2012 12:14
Is string match pattern
<?php
function str_is_match($pattern, $string)
{
$array = null;
$res = preg_match_all('('.$pattern.')', $string, $array);
return !(($res === false) or ($res == 0) or ($res > 1) or ($array[0][0] != $string));
}
@r37r0m0d3l
r37r0m0d3l / translit.js
Created September 12, 2012 12:16
Translit
var ru2en =
{
ru_str : "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя",
en_str : ['A','B','V','G','D','E','JO','ZH','Z','I','J','K','L','M','N','O','P','R','S','T',
'U','F','H','C','Ch','Sh','Shh',String.fromCharCode(35),'I',String.fromCharCode(39),'Je','Ju',
'Ja','a','b','v','g','d','e','jo','zh','z','i','j','k','l','m','n','o','p','r','s','t','u','f',
'h','c','ch','sh','shh',String.fromCharCode(35),'i',String.fromCharCode(39),'je','ju','ja'],
translit : function(org_str)
{
var tmp_str = "";
@r37r0m0d3l
r37r0m0d3l / mime_type.php
Last active October 10, 2015 14:47
Get file mime type
<?php
class mime_type
{
static function url_mime_type($url)
{
$finfo = new \finfo(FILEINFO_MIME_TYPE);
return $finfo->buffer(file_get_contents($url));
};
static function file_mime_type($file_name)
@r37r0m0d3l
r37r0m0d3l / file_download.php
Created September 12, 2012 12:20
File download
<?php
function download_file($url, $newfname)
{
if (($file = fopen($url, 'rb')))
if (($newf = fopen($newfname, 'wb')))
while (!feof($file))
fwrite($newf, fread($file, 8192), 8192);
if ($file)
fclose($file);
if (isset($newf))
@r37r0m0d3l
r37r0m0d3l / mail_html.php
Created September 12, 2012 12:21
Mail as html
<?php
function mail($to, $person, $subject, $message, $from_name, $from_mail)
{
$headers = 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=utf-8'."\r\n";
$headers .= 'To: '.$person.' <'.$to.'>'."\r\n";
$headers .= 'From: '.$from_name.' <'.$from_mail.'>'."\r\n";
return mail($to, $subject, $message, $headers);
}