46 个非常有用的 PHP 代码片段(2)


5. 获取 Web 页面的源代码
使用下面的函数,可以获取任意 Web 页面的 HTML 代码

function display_sourcecode($url) { $lines = file($url); $output = ""; foreach ($lines as $line_num => $line) { // loop thru each line and prepend line numbers $output.= "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "\n"; } }

语法:

<?php $url = "https://blog.koonk.com"; $source = display_sourcecode($url); echo $source; ?>


6. 计算喜欢你的 Facebook 页面的用户

function fb_fan_count($facebook_name) { $data = json_decode(file_get_contents("https://graph.facebook.com/".$facebook_name)); $likes = $data->likes; return $likes; }

语法:

<?php $page = "koonktechnologies"; $count = fb_fan_count($page); echo $count; ?>


7. 确定任意图片的主导颜色

function dominant_color($image) { $i = imagecreatefromjpeg($image); for ($x=0;$x<imagesx($i);$x++) { for ($y=0;$y<imagesy($i);$y++) { $rgb = imagecolorat($i,$x,$y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> & 0xFF; $b = $rgb & 0xFF; $rTotal += $r; $gTotal += $g; $bTotal += $b; $total++; } } $rAverage = round($rTotal/$total); $gAverage = round($gTotal/$total); $bAverage = round($bTotal/$total); }


8. whois 查询
使用下面的函数可以获取任何域名用户的完整细节

function whois_query($domain) { // fix the domain name: $domain = strtolower(trim($domain)); $domain = preg_replace('/^http:\/\//i', '', $domain); $domain = preg_replace('/^www\./i', '', $domain); $domain = explode('https://www.jb51.net/', $domain); $domain = trim($domain[0]); // split the TLD from domain name $_domain = explode('.', $domain); $lst = count($_domain)-1; $ext = $_domain[$lst]; // You find resources and lists // like these on wikipedia: // // // $servers = array( "biz" => "whois.neulevel.biz", "com" => "whois.internic.net", "us" => "whois.nic.us", "coop" => "whois.nic.coop", "info" => "whois.nic.info", "name" => "whois.nic.name", "net" => "whois.internic.net", "gov" => "whois.nic.gov", "edu" => "whois.internic.net", "mil" => "rs.internic.net", "int" => "whois.iana.org", "ac" => "whois.nic.ac", "ae" => "whois.uaenic.ae", "at" => "whois.ripe.net", "au" => "whois.aunic.net", "be" => "whois.dns.be", "bg" => "whois.ripe.net", "br" => "whois.registro.br", "bz" => "whois.belizenic.bz", "ca" => "whois.cira.ca", "cc" => "whois.nic.cc", "ch" => "whois.nic.ch", "cl" => "whois.nic.cl", "cn" => "whois.cnnic.net.cn", "cz" => "whois.nic.cz", "de" => "whois.nic.de", "fr" => "whois.nic.fr", "hu" => "whois.nic.hu", "ie" => "whois.domainregistry.ie", "il" => "whois.isoc.org.il", "in" => "whois.ncst.ernet.in", "ir" => "whois.nic.ir", "mc" => "whois.ripe.net", "to" => "whois.tonic.to", "tv" => "whois.tv", "ru" => "whois.ripn.net", "org" => "whois.pir.org", "aero" => "whois.information.aero", "nl" => "whois.domain-registry.nl" ); if (!isset($servers[$ext])){ die('Error: No matching nic server found!'); } $nic_server = $servers[$ext]; $output = ''; // connect to whois server: if ($conn = fsockopen ($nic_server, 43)) { fputs($conn, $domain."\r\n"); while(!feof($conn)) { $output .= fgets($conn,128); } fclose($conn); } else { die('Error: Could not connect to ' . $nic_server . '!'); } return $output; }


语法:

<?php $domain = "http://www.blog.koonk.com"; $result = whois_query($domain); print_r($result); ?>


9. 验证邮箱地址
有时候,当在网站填写表单,用户可能会输入错误的邮箱地址,这个函数可以验证邮箱地址是否有效。

function is_validemail($email) { $check = 0; if(filter_var($email,FILTER_VALIDATE_EMAIL)) { $check = 1; } return $check; } 语法: <?php $email = "blog@koonk.com"; $check = is_validemail($email); echo $check; // If the output is 1, then email is valid. ?>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/6b63572ee0f0c42def825373c5cfbc86.html