私のプロジェクトで使用するいくつかの機能

良い日、カットの下で、私のプロジェクトのほとんどで使用しているPHPの関数に慣れることをお勧めします。 記事では、Googleを使用して世界のあらゆる都市の天気を取得し、Whoisおよびfaviconドメイン、特定のページのリツイ​​ート数を取得し、Twitterでプロフィールへのリンクのジェネレーターを作成し、サイトのスクリーンショットを撮り、CSSをYandexのような1つのファイルに収集し、zipを解凍し、写真を変換しますASCIIコード。



Google API経由の天気予報



今日の天気はどうですか? これらの3行のコードは、調べるのに役立ちます。 必要なのは、最初の行のADDRESSを自分の住所に置き換えることだけです。

$xml = simplexml_load_file('http://www.google.com/ig/api?weather=ADDRESS'); $information = $xml -> xpath("/xml_api_reply/weather/current_conditions/condition"); echo $information[0]->attributes();
      
      





シンプルなPHP whois



Whoisサービスは、特定のドメインに関するさまざまな情報(所有者、作成時間、登録など)を知りたい場合に非常に便利です。 UNIXからPHP whoisコマンドを使用すると、同様の関数を簡単に作成できます。 whoisコマンドはWebサーバーでサポートされている必要があります。サポートされていない場合、何も機能しません。

 $domains = array('home.pl', 'w3c.org'); function creation_date($domain) { $lines = explode("\n", `whois $domain`); foreach($lines as $line) { if(strpos(strtolower($line), 'created') !== false) { return $line; } } return false; } foreach($domains as $d) { echo creation_date($d) . "\n"; }
      
      





PHPとGoogleを使用してファビコンを取得する



最近では、ウェブサイトでサードパーティのファビコンがよく使用されています。 この問題を解決するには、GoogleとPHPが役立ちます。

 function get_favicon($url) { $url = urlencode(str_replace("http://","",$url)); return 'http://www.google.com/s2/favicons?domain='.$url; }
      
      





ハリネズミありがとう

PHPの特定のページのリツイ​​ート数を取得する



特定のページにリツイートカウンターを使用したいですか? これは、Tweetmeme APIを使用してPHPに実装することは難しくありません。

 function tweetCount($url) { $content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url); $element = new SimpleXmlElement($content); $retweets = $element->story->url_count; if($retweets) { return $retweets; } else { return 0; } }
      
      





「@nick」形式のテキストをリンクに変換します(Twitterなど)



 function parseTwitterNicks($str, $allowed = 'all', $format = 'default', $toArray = false){ preg_match_all('~@([a-z0-9-_]+)~is', $str, $match); if($format == 'default') $format = 'profile.php?user={nick}'; if(!preg_match('~\{nick\}~', $format)) $format = $format . '{nick}'; if(empty($match[1])) return ($toArray ? array() : $str); $found = array(); foreach($match[1] as $nick) { if(!empty($allowed) && $allowed != 'all') { if(is_array($allowed)) { if(!in_array($nick, $allowed)) continue; } } $url = str_replace('{nick}', $nick, $format); $str = str_replace('@' . $nick, '<a href="' . $url . '" title="' . $nick . '">@' . $nick . '</a>', $str); $found[] = $nick; } return ($toArray ? $found : $str); }
      
      





サイトのスクリーンショットを作成する



 function screen($url, $razr, $razm, $form) { $toapi="http://mini.s-shot.ru/".$razr."/".$razm."/".$form."/?".$url; $scim=file_get_contents($toapi); file_put_contents("screen.".$form, $scim); }
      
      





関数呼び出し:
 screen("http://habr.ru", "1024x768", "600", "jpeg");
      
      





複数のCSSファイルを1つにまとめる



サイトで複数のCSSファイルを使用すると、サイト全体の読み込み時間が長くなります。

このスクリプトを使用すると、スタイルを圧縮できます。

 header('Content-type: text/css'); ob_start("compress"); function compress($buffer) { $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); return $buffer; } include('style1.css'); include('style2.css'); include('template_style1.css'); include('template_style2.css'); include('print.css'); ob_end_flush();
      
      





サーバーでzipアーカイブを解凍します



 function unzip($location,$newLocation){ if(exec("unzip $location",$arr)) { mkdir($newLocation); for($i = 1;$i< count($arr);$i++) { $file = trim(preg_replace("~inflating: ~","",$arr[$i])); copy($location.'/'.$file,$newLocation.'/'.$file); unlink($location.'/'.$file); } return TRUE; } else { return FALSE; } }
      
      





そして、関数を呼び出します

 if(unzip('uploads/test.zip','uploads/unziped/test')) echo ' '; else echo '';
      
      





任意のJPGイメージからASCIIコードを作成します



 <style>body { line-height:1px;font-size:1px; }</style> <?php function getext($filename) { $pos = strrpos($filename,'.'); $str = substr($filename, $pos); return $str; } $image = 'image.jpg'; $ext = getext($image); if($ext == ".jpg") { $img = ImageCreateFromJpeg($image); } else { echo '  JPG'; } $width = imagesx($img); $height = imagesy($img); for($h=0;$h<$height;$h++) { for($w=0;$w<=$width;$w++) { $rgb = ImageColorAt($img, $w, $h); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; if($w == $width) { echo '<br>'; } else { echo '<span style="color:rgb('.$r.','.$g.','.$b.');">#</span>'; } } } ?>
      
      






All Articles