Простоузер
Join Date: Aug 2013
Posts: 36
Версия vB: 3.8.7
Reputation:
Novice 3
Репутация в разделе: 3
|
Полезные функции/скрипты PHP
4
Функция очистки скрипта от комментариев:
Code:
function remove_comments($code) {
$array = token_get_all($code);
$ret = "";
for($i = 0; $i < count($array); $i++)
{
$obj = $array[$i];
if(is_string($obj))
{
$ret .= $obj;
}
else
{
if(!strstr(token_name($obj[0]),"COMMENT"))
$ret .= $obj[1];
}
}
return $ret;
}
Транслит
Code:
function totranslit($var, $lower = true, $punkt = true) { $NpjLettersFrom = "абвгдезиклмнопрстуфцыі";
$NpjLettersTo = "abvgdeziklmnoprstufcyi";
$NpjBiLetters = array ("й" => "j", "ё" => "yo", "ж" => "zh", "х" => "x", "ч" => "ch", "ш" => "sh", "щ" => "shh", "э" => "ye", "ю" => "yu", "я" => "ya", "ъ" => "", "ь" => "'", "ї" => "yi", "є" => "ye" );
$NpjCaps = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЫЭЮЯЇЄІ";
$NpjSmall = "абвгдеёжзийклмнопрстуфхцчшщьъыэюяїєі";
$var = str_replace( ".php", "[точко]пэхопэ", $var );
$var = trim( strip_tags( $var ) );
$var = preg_replace( "/\s+/ms", "-", $var );
$var = strtr( $var, $NpjCaps, $NpjSmall );
$var = strtr( $var, $NpjLettersFrom, $NpjLettersTo );
$var = strtr( $var, $NpjBiLetters );
if ( $punkt ) $var = preg_replace( "/[^a-z0-9\_\-.]+/mi", "", $var );
else $var = preg_replace( "/[^a-z0-9\_\-]+/mi", "", $var );
$var = preg_replace( '#[\-]+#i', '-', $var );
if ( $lower ) $var = strtolower( $var );
if( strlen( $var ) > 50 ) {
$var = substr( $var, 0, 50 );
if( ($temp_max = strrpos( $var, '-' )) ) $var = substr( $var, 0, $temp_max );
}
return $var;
}
Очистка ссылки от мусора
Code:
function clean_url($url) {
if( $url == '' ) return;
$url = str_replace( "http://", "", strtolower( $url ) );
if( substr( $url, 0, 4 ) == 'www.' ) $url = substr( $url, 4 );
$url = explode( '/', $url );
$url = reset( $url );
$url = explode( ':', $url );
$url = reset( $url );
return $url;
}
Проверяем не с кпк\телефона ли зашел пользователь
Code:
function checkSmartphone() {
$phone_array = array('iphone', 'pocket', 'palm', 'windows ce', 'windowsce', 'cellphone', 'opera mobi', 'ipod', 'small', 'sharp', 'sonyericsson', 'symbian', 'opera mini', 'nokia', 'htc_', 'samsung', 'motorola', 'smartphone', 'blackberry', 'playstation portable', 'tablet browser');
$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
foreach ($phone_array as $value) {
if ( strpos($agent, $value) !== false ) return true;
}
return false;
}
Проверка Тиц
Code:
<?
function getTIC($site) {
$url = 'http://'.$site;
$str=@file('http://bar-navig.yandex.ru/u?ver=2&show=32&url='.$url);
if ($str==false) {
$ans=false; }
else {
$is_find=preg_match("/value=\"(.\d*)\"/", join("",$str), $tic);
if ($is_find<1) {
$ans=0; }
else {
$ans=$tic[1]; }
}
return $ans;
}
print 'Тиц: '.getTIC("site.ru");
?>
Класс для работы с комплексными числами. Позволяет прибавлять, отнимать, умножать и делить комплексные числа.
class.complex.php
Code:
<?php
///////////////////////////////////////////////////////////////
// Reza Salehi
// zaalion @ yahoo.com
// free for non-commercial use.
///////////////////////////////////////////////////////////////
class complex
{
function complex($first, $second)
{
$this->first['re']=$first['re'];
$this->first['im']=$first['im'];
$this->second['re']=$second['re'];
$this->second['im']=$second['im'];
}
function add()
{
$result['re']=$this->first['re']+$this->second['re'];
$result['im']=$this->first['im']+$this->second['im'];
return($result);
}
function sub()
{
$result['re']=$this->first['re']-$this->second['re'];
$result['im']=$this->first['im']-$this->second['im'];
return($result);
}
function mul()
{
$result['re']=($this->first['re']*$this->second['re'])-($this->first['im']*$this->second['im']);
$result['im']=($this->first['re']*$this->second['im'])+($this->second['re']*$this->first['im']);
return($result);
}
function div()
{
if(($this->second['re']==0 && $this->second['im']==0))
retuen(-1);
$a=$this->first['re'];
$b=$this->first['im'];
$c=$this->second['re'];
$d=$this->second['im'];
@$result['re']=((a*c+b*d)/(c*c-d*d));
@$result['im']=((b*c-a*d)/(c*c-d*d));
return($result);
}
}
?>
Класс для определения рейтинга сайта в Alexa.
Alexa.class.php
Code:
<?php
/**
* @author [YS.PRO]
* @copyright Copyright © 2009, [YS.PRO]
* http://ys-pro.com
* @version 0.2
*/
class Alexa {
const CURL_TIMEOUT = 20;
const ALEXA_SITE_INFO_URL = 'http://www.alexa.com/siteinfo/';
private $domain = NULL;
public function __construct($domain = NULL) {
if (!is_null($domain)) {
$this->domain = $domain;
} else {
throw new Exception('You must pass domain name to constructor!');
}
}
public function setDomain($domain) {
$this->domain = $domain;
}
public function getAlexaRank() {
$response = $this->get(self::ALEXA_SITE_INFO_URL . $this->domain);
// parse string with alexa ranking info
$regexp = '#<div class="data .+?">(.*?)</div>#si';
preg_match($regexp, $response, $matches);
if (!isset($matches[1])) {
return FALSE;
}
preg_match('#[\d,]+#s', $matches[1], $m);
if (!isset($m[0])) {
return FALSE;
}
$rank = $m[0];
// delete commas
$rank = str_replace(',', '', $rank);
return (int) $rank;
}
protected function get($url) {
$hCurl = curl_init($url);
curl_setopt($hCurl, CURLOPT_TIMEOUT, self::CURL_TIMEOUT);
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, TRUE);
return curl_exec($hCurl);
}
}
?>
|