PHP функции

(3 оценок, среднее: 3,00 из 5)
Загрузка...

Определение MAC адреса

<?php
function definition_mac() {
 
 if(PHP_OS == 'Linux'){  $macAddr = exec("grep ".$_SERVER['REMOTE_ADDR']." /proc/net/arp | awk '{print $4}'");  }
 elseif(PHP_OS == 'WINNT'){
 
  $ipAddress=$_SERVER['REMOTE_ADDR'];
  $macAddr=false;
  $arp=`arp -a $ipAddress`;
  $lines=explode("\n", $arp);
 
  foreach($lines as $line){
    $cols=preg_split('/\s+/', trim($line));
    if ($cols[0]==$ipAddress){ $macAddr=$cols[1]; }
  }
 };
 return $macAddr; 
};
// Выводит MAC-адрес. Работает только в локальной сети.
?>

Функция на маштобированния изображения

<?php
function resize_img($file_input, $file_output, $w_o, $h_o , $percent = false) {
  list($w_i, $h_i, $type) = getimagesize($file_input);
  if (!$w_i || !$h_i) {
   echo 'Ошибка 1';
   return;
  }
  
  $types = array('','gif','jpeg','png');
  $ext = $types[$type];
  if ($ext) {
    $func = 'imagecreatefrom'.$ext;
    $img = $func($file_input);
  } else {
   echo 'Ошибка 2';
   return;
  }

  if ($percent) {
    $w_o *= $w_i / 100;
    $h_o *= $h_i / 100;
  }

  if (!$h_o) $h_o = $w_o/($w_i/$h_i);
  if (!$w_o) $w_o = $h_o/($h_i/$w_i);
  $img_o = imagecreatetruecolor($w_o, $h_o);
  imagecopyresampled($img_o, $img, 0, 0, 0, 0, $w_o, $h_o, $w_i, $h_i);
  if ($type == 2) {
   return imagejpeg($img_o,$file_output,100);
  } else {
  $func = 'image'.$ext;
  return $func($img_o,$file_output);
  }
 }
?>

Транслит из кириллицы с заменой спецсимволов

<?php
function translitIt($str) {
 $tr = array(
 "А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
 "Д"=>"D","Е"=>"E","Ё"=>"E","Ж"=>"J","З"=>"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","ё"=>"e","ж"=>"j",
 "з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
 "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
 "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
 "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y",
 "ы"=>"yi","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya"," "=>"_","#"=>"_"
 ,"№"=>"_",";"=>"_","$"=>"_","~"=>"_","`"=>"_","'"=>"_"
 ,"*"=>"_","&"=>"_","?"=>"_","!"=>"_","@"=>"_","="=>"_"
 ,"+"=>"_","-"=>"_","^"=>"_","%"=>"_",","=>"_"
 ,":"=>"_","{"=>"_","}"=>"_","["=>"_","]"=>"_"
 );
 return strtr($str,$tr);
}
?>

Работа с датами

<?php
echo date('d.m.Y H:i' , strtotime( '2015-07-01 12:15:00' ));
// Выводит "01.07.2015 12:15"

echo date( "Y-m-d", strtotime( "2009-01-31 +1 month" ) );
?>

Число с ведущими нулями

<?php
$str = '1';
echo sprintf("%04d", $str);
// Выводит "0001"
?>

YouTube

https://www.youtube.com/watch?v=VS9qxV6UPC0 -> //www.youtube.com/embed/VS9qxV6UPC0
function convert_youtube($url){
  parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
  $youtube= '<iframe src="//www.youtube.com/embed/'.$my_array_of_vars['v'].'" 
                  width="150px" 
                  height="150px" 
                  frameborder="0" 
                  allowfullscreen="allowfullscreen">
             </iframe>';
  return $youtube;
}