19个超实用的PHP代码片段(3)

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——检测服务器是否是HTTPS

复制代码 代码如下:

if ($_SERVER['HTTPS'] != "on") {  
    echo "This is not HTTPS"; 
}else{ 
    echo "This is HTTPS"; 


9) Generate CSV file from a PHP array——在PHP数组中生成.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; 

10.查找Longitudes与Latitudes之间的距离

复制代码 代码如下:

function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) { 
    $theta = $longitude1 - $longitude2; 
    $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta))); 
    $miles = acos($miles); 
    $miles = rad2deg($miles); 
    $miles = $miles * 60 * 1.1515; 
    $feet = $miles * 5280; 
    $yards = $feet / 3; 
    $kilometers = $miles * 1.609344; 
    $meters = $kilometers * 1000; 
    return compact('miles','feet','yards','kilometers','meters');  


$point1 = array('lat' => 40.770623, 'long' => -73.964367); 
$point2 = array('lat' => 40.758224, 'long' => -73.917404); 
$distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'], $point2['lat'], $point2['long']); 
foreach ($distance as $unit => $value) { 
    echo $unit.': '.number_format($value,4).' 
'; 
}



The example returns the following: 

复制代码 代码如下:

miles: 2.6025 
feet: 13,741.4350 
yards: 4,580.4783 
kilometers: 4.1884 
meters: 4,188.3894

11.完善cURL功能

复制代码 代码如下:

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

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