📜 ⬆️ ⬇️

Some features that I use in my projects

Good day, Under the cat I suggest you familiarize yourself with the functions in PHP that I use in most of my projects. In the article we will get the weather for any city in the world using Google, get Whois and domain favicon, the number of retweets of a certain page and make a link generator to the profiles on Twitter, take a screenshot of the site, compile css into 1 file like Yandex, unpack the zip and convert the picture in ASCII code.

Weather forecast via Google API


Do you know what the weather is today? These three lines of code will help you find out. All you need to do is replace ADDRESS with your address in the first line.
$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(); 

Simple PHP whois


The Whois service is very useful for those who want to find out various information about a particular domain: the owner, the time of creation, registration, etc. Using PHP unix whois command is very easy to write a similar function. Note that the whois command must be supported by your web server, otherwise it will not work.
 $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"; } 

Get favicon using PHP and Google


These days, websites often use third-party favicon. To solve this problem, Google and PHP will help us.
 function get_favicon($url) { $url = urlencode(str_replace("http://","",$url)); return 'http://www.google.com/s2/favicons?domain='.$url; } 

Thank you hedgehog

Get the number of retweets of a certain page in PHP


Want to use your specific page retweet count? This is not difficult to implement in PHP using the Tweetmeme API.
 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; } } 

Text format "@nick" convert to a link (like 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); } 

Create a screenshot of the site


 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); } 

Function call:
 screen("http://habr.ru", "1024x768", "600", "jpeg"); 

Putting several CSS files in one


If you use several CSS files on your site, they increase the loading time of the entire site.
With this script you can compress your styles.
 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(); 

Extract the zip archive on the server


 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; } } 

And call our function
 if(unzip('uploads/test.zip','uploads/unziped/test')) echo ' '; else echo ''; 

Make an ASCII code from any JPG image


 <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>'; } } } ?> 

')

Source: https://habr.com/ru/post/130196/


All Articles