您的当前位置:首页正文

19个超实用的PHP代码片段

2024-11-03 来源:个人技术集锦

1) Whois query using PHP ——利用PHP获取Whois请求

TextMagic引入强大的核心API,可轻松将SMS发送到手机。该API是需要付费。

代码如下:

the TextMagic PHP lib 
require('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——获取内存使用率

这段代码帮助你获取内存使用率。

代码如下:

echo "Initial: ".memory_get_usage()." bytes \n"; 
/* prints
Initial: 361400 bytes
*/ 

// let's use up some memory 
for ($i = 0; $i < 100000; $i++) { 
    $array []= md5($i); 


// let's remove half of the array 
for ($i = 0; $i < 100000; $i++) { 
    unset($array[$i]); 


echo "Final: ".memory_get_usage()." bytes \n"; 
/* prints
Final: 885912 bytes
*/ 

echo "Peak: ".memory_get_peak_usage()." bytes \n"; 
/* prints
Peak: 13687072 bytes
*/ 

4) Display source code of any webpage——查看任意网页源代码

检测浏览器使用的代码脚本语言。

代码如下:
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

var_dump(password_strength("Correct Horse Battery Staple"));
echo "<br>";
var_dump(password_strength("Super Monkey Ball"));
echo "<br>";
var_dump(password_strength("Tr0ub4dor&3"));
echo "<br>";
var_dump(password_strength("abc123"));
echo "<br>";
var_dump(password_strength("sweet"));

Top