Skip to content

Instantly share code, notes, and snippets.

View Foxy79's full-sized avatar

Aleksey Borisov Foxy79

View GitHub Profile
@Foxy79
Foxy79 / bom.php
Created October 5, 2016 08:56
BOM remove from json answer
<?php
$bom = pack('H*','EFBBBF');
$json = preg_replace("/^$bom/", '', $json);
@Foxy79
Foxy79 / phone.php
Created April 11, 2016 14:32
Форматирование номера телефона
<?php
function format_phone($phone = '', $convert = false, $trim = true) {
if (empty($phone)) return '';
$phone = preg_replace("/[^0-9A-Za-z]/", "", $phone);
if ($convert == true) {
$replace = array('2'=>array('a','b','c'),'3'=>array('d','e','f'),'4'=>array('g','h','i'),'5'=>array('j','k','l'),'6'=>array('m','n','o'),'7'=>array('p','q','r','s'),'8'=>array('t','u','v'), '9'=>array('w','x','y','z'));
foreach($replace as $digit=>$letters) {
$phone = str_ireplace($letters, $digit, $phone);
}
}
@Foxy79
Foxy79 / dropbox-webhook.php
Created March 18, 2016 09:57
DropBox WebHook PHP Sample
<?php
define ('SECRET', 'Your_secret_here');
if (isset($_GET['challenge'])) {
echo $_GET['challenge'];
} else {
$raw_data = file_get_contents('php://input');
if ($raw_data) {
$json = json_decode($raw_data);
if (is_object($json)) {
@Foxy79
Foxy79 / adblock.js
Created March 3, 2016 09:53
Adblock detect
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
@Foxy79
Foxy79 / geo.php
Created February 26, 2016 08:58
Функция для расчета расстояния в метрах между 2мя точками по координатам
<?php
function locDistance($lat1, $lon1, $lat2, $lon2) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return ($miles * 1.609344 * 1000);
@Foxy79
Foxy79 / geo.sql
Created February 26, 2016 08:56
Mysql sample query finding points with distanse from current coords 500m
-- 55.88846600 = current latitude
-- 37.44916300 = current longitude
SELECT Title, ROUND( 6371000 * acos( cos( radians('55.88846600') ) * cos( radians( Lat ) ) * cos( radians( Lng ) - radians('37.44916300') ) + sin( radians('55.88846600') ) * sin( radians( Lat ) ) ) ) AS distance FROM Points HAVING distance < 500 OR distance IS NULL ORDER BY distance LIMIT 1
<?php
if (!function_exists('getallheaders')) {
function getallheaders() {
$headers = '';
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
<?php
function ev_mb_ucfirst($str) {
$fc = mb_strtoupper(mb_substr($str, 0, 1));
return $fc.mb_substr($str, 1);
}
@Foxy79
Foxy79 / obj2arr.php
Last active February 24, 2016 14:26
<?php
function object_to_array($data) {
if (is_array($data) || is_object($data)) {
$result = array();
foreach ($data as $key => $value) { $result[$key] = object_to_array($value); }
return $result;
}
return $data;
}
<?php
class db {
public $isConnected;
protected $datab;
public function __construct($username, $password, $host, $dbname, $options=array()){
$this->isConnected = true;
try {
$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);