成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

直接拿來(lái)用 九個(gè)超實(shí)用的PHP代碼片段(二)

瀏覽:4日期:2022-09-13 18:02:07

每位程序員和開(kāi)發(fā)者都喜歡討論他們最愛(ài)的代碼片段,尤其是當(dāng)PHP開(kāi)發(fā)者花費(fèi)數(shù)個(gè)小時(shí)為網(wǎng)頁(yè)編碼或創(chuàng)建應(yīng)用時(shí),他們更知道這些代碼的重要性。為了節(jié)約編碼時(shí)間,筆者收集了一些較為實(shí)用的代碼片段,幫助開(kāi)發(fā)者提高工作效率。>>> 點(diǎn)擊查看PHP代碼片段(一)

直接拿來(lái)用 九個(gè)超實(shí)用的PHP代碼片段(二)

1) Whois query using PHP ——利用PHP獲取Whois請(qǐng)求

利用這段代碼,在特定的域名里可獲得whois信息。把域名名稱作為參數(shù),并顯示所有域名的相關(guān)信息。

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(’/’, $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: // // <a >http://de.wikipedia.org/wiki/Whois</a> // $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.'rn');while(!feof($conn)) { $output .= fgets($conn,128);}fclose($conn); } else { die(’Error: Could not connect to ’ . $nic_server . ’!’); } return $output;}2) Text messaging with PHP using the TextMagic API ——使用TextMagic API 獲取PHP Test信息

TextMagic引入強(qiáng)大的核心API,可輕松將SMS發(fā)送到手機(jī)。該API是需要付費(fèi)。

the TextMagic PHP librequire(’textmagic-sms-api-php/TextMagicAPI.php’);// Set the username and password information$username = ’myusername’;$password = ’mypassword’;// Create a new instance of TM$router = new TextMagicAPI(array(’username’ => $username,’password’ => $password));// Send a text message to ’999-123-4567’$result = $router->send(’Wake up!’, array(9991234567), true);// result: Result is: Array ( [messages] => Array ( [19896128] => 9991234567 ) [sent_text] => Wake up! [parts_count] => 1 )3) Get info about your memory usage——獲取內(nèi)存使用率

這段代碼幫助你獲取內(nèi)存使用率。

echo 'Initial: '.memory_get_usage().' bytes n';/* printsInitial: 361400 bytes*/// let’s use up some memoryfor ($i = 0; $i < 100000; $i++) {$array []= md5($i);}// let’s remove half of the arrayfor ($i = 0; $i < 100000; $i++) {unset($array[$i]);}echo 'Final: '.memory_get_usage().' bytes n';/* printsFinal: 885912 bytes*/echo 'Peak: '.memory_get_peak_usage().' bytes n';/* printsPeak: 13687072 bytes*/4) Display source code of any webpage——查看任意網(wǎng)頁(yè)源代碼

如果你想查看網(wǎng)頁(yè)源代碼,那么只需更改第二行的URL,源代碼就會(huì)在網(wǎng)頁(yè)上顯示出。

<?php // display source code $lines = file(’http://google.com/’); foreach ($lines as $line_num => $line) { // loop thru each line and prepend line numbersecho 'Line #{$line_num} : ' . htmlspecialchars($line) . 'n';}5) Create data uri’s——?jiǎng)?chuàng)建數(shù)據(jù)uri

通過(guò)使用此代碼,你可以創(chuàng)建數(shù)據(jù)Uri,這對(duì)在HTML/CSS中嵌入圖片非常有用,可幫助節(jié)省HTTP請(qǐng)求。

function data_uri($file, $mime) { $contents=file_get_contents($file); $base64=base64_encode($contents); echo 'data:$mime;base64,$base64';}6) Detect location by IP——通過(guò)IP檢索出地理位置

這段代碼幫助你查找特定的IP,只需在功能參數(shù)上輸入IP,就可檢測(cè)出位置。

function detect_city($ip) {$default = ’UNKNOWN’;if (!is_string($ip) || strlen($ip) < 1 || $ip == ’127.0.0.1’ || $ip == ’localhost’) $ip = ’8.8.8.8’; $curlopt_useragent = ’Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)’; $url = ’http://ipinfodb.com/ip_locator.php?ip=’ . urlencode($ip); $ch = curl_init(); $curl_opt = array( CURLOPT_FOLLOWLOCATION => 1, CURLOPT_HEADER => 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_USERAGENT => $curlopt_useragent, CURLOPT_URL => $url, CURLOPT_TIMEOUT => 1, CURLOPT_REFERER => ’http://’ . $_SERVER[’HTTP_HOST’],);curl_setopt_array($ch, $curl_opt);$content = curl_exec($ch);if (!is_null($curl_info)) { $curl_info = curl_getinfo($ch);}curl_close($ch);if ( preg_match(’{City : ([^<]*)}i’, $content, $regs) ) { $city = $regs[1]; } if ( preg_match(‘{State/Province : ([^<]*) }i’, $content, $regs) ) { $state = $regs[1]; } if( $city!=” && $state!=” ){ $location = $city . ‘, ‘ . $state; return $location; }else{ return $default; } }7) Detect browser language——查看瀏覽器語(yǔ)言

檢測(cè)瀏覽器使用的代碼腳本語(yǔ)言。

function get_client_language($availableLanguages, $default=’en’){if (isset($_SERVER[’HTTP_ACCEPT_LANGUAGE’])) {$langs=explode(’,’,$_SERVER[’HTTP_ACCEPT_LANGUAGE’]);foreach ($langs as $value){$choice=substr($value,0,2);if(in_array($choice, $availableLanguages)){return $choice;}}} return $default;}8) Check if server is HTTPS——檢測(cè)服務(wù)器是否是HTTPS

if ($_SERVER[’HTTPS’] != 'on') { echo 'This is not HTTPS';}else{echo 'This is HTTPS';}9) Generate CSV file from a PHP array——在PHP數(shù)組中生成.csv 文件

function generateCsv($data, $delimiter = ’,’, $enclosure = ’'’) { $handle = fopen(’php://temp’, ’r+’); foreach ($data as $line) { fputcsv($handle, $line, $delimiter, $enclosure); } rewind($handle); while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); return $contents;}

英文出自:Designzum

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 亚洲高清一区二区三区四区 | 国产精品久久毛片 | 色内内免费视频播放 | 久久久在线视频精品免费观看 | 国产日韩精品一区二区在线观看 | 青青热久久国产久精品秒播 | 国产成在线观看免费视频 | 久久在线免费观看视频 | videos性欧美| 欧美成人香蕉网在线观看 | 欧美成人午夜不卡在线视频 | 国产成人综合网在线观看 | 亚洲精品 欧美 | 中国人免费观看高清在线观看二区 | 亚洲成人黄色在线观看 | 欧美激情中文字幕 | 久久久久久久久影院 | 色偷偷亚洲偷自拍 | 97免费公开视频 | 日韩黄色一级片 | 精品国产91久久久久久久a | a毛片在线还看免费网站 | 深夜福利国产福利视频 | 成人午夜网站 | 99久久国产综合精品五月天 | 精品色视频| 永久网站色视频在线观看免费 | 亚洲网址在线观看 | 亚洲黄色在线播放 | 久在线视频 | 九九国产在线观看 | 97在线免费视频 | 久久精品国产精品青草 | 久草视频在线免费看 | 91精品国产欧美一区二区 | 在线观看国产一区二区三区99 | 二级黄的全免费视频 | 波多野一区二区三区在线 | 和日本免费不卡在线v | 亚洲在线视频一区 | 一级做a爰片性色毛片中国 一级做a爰性色毛片 |