《PHP教程:分享10段PHP常用代碼》要點:
本文介紹了PHP教程:分享10段PHP常用代碼,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP學(xué)習(xí)本文匯集PHP開發(fā)中經(jīng)常用到的十段代碼,包括Email、64位編碼和解碼、解壓縮、64位編碼、解析JSON等,希望對您有所贊助.
PHP學(xué)習(xí)1、使用PHP Mail函數(shù)發(fā)送Email
PHP學(xué)習(xí)
$to = "viralpatel.net@gmail.com";
$subject = "VIRALPATEL.net";
$body = "Body of your message here you can use HTML too. e.g. br b Bold /b";
$headers = "From: Peter\r\n";
$headers .= "Reply-To: info@yoursite.com\r\n";
$headers .= "Return-Path: info@yoursite.com\r\n";
$headers .= "X-Mailer: PHP5\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$body,$headers);
?
PHP學(xué)習(xí)2、PHP中的64位編碼息爭碼
PHP學(xué)習(xí)
function base64url_encode($plainText) {
$base64 = base64_encode($plainText);
$base64url = strtr($base64, '+/=', '-_,');
return $base64url;
}
function base64url_decode($plainText) {
$base64url = strtr($plainText, '-_,', '+/=');
$base64 = base64_decode($base64url);
return $base64;
}
PHP學(xué)習(xí)3、獲取長途IP地址
PHP學(xué)習(xí)
function getRealIPAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
PHP學(xué)習(xí)4、 日期格局化
PHP學(xué)習(xí)
function checkDateFormat($date)
{
//match the format of the date
if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
{
//check weather the date is valid of not
if(checkdate($parts[2],$parts[3],$parts[1]))
return true;
else
return false;
}
else
return false;
}
PHP學(xué)習(xí)5、驗證Email
PHP學(xué)習(xí)
$email = $_POST['email'];
if(preg_match("~([a-zA-Z0-9!#$%&'*+-/=?^_`{|}~])@([a-zA-Z0-9-]).
([a-zA-Z0-9]{2,4})~",$email)) {
echo 'This is a valid email.';
} else{
echo 'This is an invalid email.';
}
PHP學(xué)習(xí)6、在PHP中輕松解析XML
PHP學(xué)習(xí)
//this is a sample xml string
$xml_string="?xml version='1.0'?
moleculedb
molecule name='Benzine'
symbolben/symbol
codeA/code
/molecule
molecule name='Water'
symbolh2o/symbol
codeK/code
/molecule
/moleculedb";
//load the xml string using simplexml function
$xml = simplexml_load_string($xml_string);
//loop through the each node of molecule
foreach ($xml-molecule as $record)
{
//attribute are accessted by
echo $record['name'], ' ';
//node are accessted by - operator
echo $record-symbol, ' ';
echo $record-code, 'br /';
}
PHP學(xué)習(xí)7、數(shù)據(jù)庫連接
PHP學(xué)習(xí)
?php
if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) send_404();
$dbHost = "localhost"; //Location Of Database usually its localhost
$dbUser = "xxxx"; //Database User Name
$dbPass = "xxxx"; //Database Password
$dbDatabase = "xxxx"; //Database Name
$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or
die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");
# This function will send an imitation 404 page if the user
# types in this files filename into the address bar.
# only files connecting with in the same directory as this
# file will be able to use it as well.
function send_404()
{
header('HTTP/1.x 404 Not Found');
print '!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"'."n".
'htmlhead'."n".
'title404 Not Found/title'."n".
'/headbody'."n".
'h1Not Found/h1'."n".
'pThe requested URL '.
str_replace(strstr($_SERVER['REQUEST_URI'], '?'), '', $_SERVER['REQUEST_URI']).
' was not found on this server./p'."n".
'/body/html'."n";
exit;
}
# In any file you want to connect to the database,
# and in this case we will name this file db.php
# just add this line of php code (without the pound sign):
# include"db.php";
?
PHP學(xué)習(xí)8、創(chuàng)立和解析JSON數(shù)據(jù)
PHP學(xué)習(xí)
$json_data = array ('id'=1,'name'="rolf",'country'='russia',
"office"=array("google","oracle"));
echo json_encode($json_data);
PHP學(xué)習(xí)9、處置MySQL時間戳
PHP學(xué)習(xí)
$query = "select UNIX_TIMESTAMP(date_field) as mydate
from mytable where 1=1";
$records = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($records))
{
echo $row;
}
PHP學(xué)習(xí)10、解壓縮Zip文件
PHP學(xué)習(xí)
?php
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;
}
}
?
//Use the code as following:
?php
include 'functions.php';
if(unzip('zipedfiles/test.zip','unziped/myNewZip'))
echo 'Success!';
else
echo 'Error';
?
PHP學(xué)習(xí)PHP常用功效如下
PHP學(xué)習(xí)1.PHP字符串
PHP學(xué)習(xí)字符串聲明 變量=''或者""(一般情況會使用單引號,因為寫起來會比擬方便)
PHP學(xué)習(xí)$str = 'Hello PHP';
echo $str;
PHP學(xué)習(xí)strpos 計算字符在字符串中的地位(從0開始)
PHP學(xué)習(xí)$str = 'Hello PHP';
echo strpos($str,'o');? //計算字符在字符串中的地位
echo '<br/>';
echo strpos($str,'PH');
PHP學(xué)習(xí)substr 截取字符串
PHP學(xué)習(xí)
$str = 'Hello PHP';
//截取字符串
$str1 = substr($str,2,3); //從2地位開始截取,截取長度為3的字符串
echo $str1;
PHP學(xué)習(xí)不傳入長度參數(shù)的話,會從指定地位一直截取到字符串的末尾
PHP學(xué)習(xí)str_split 朋分字符串? 固定長度的朋分(默認(rèn)長度為1)
PHP學(xué)習(xí)
$str = 'Hello PHP';
//分割字符串
$result = str_split($str); //將結(jié)果保存到一個數(shù)組中
print_r($result); //使用print_r輸入一個數(shù)組
echo '<br/>';
$result1 = str_split($str,2);
print_r($result1);
PHP學(xué)習(xí)explode(分割字符,待分割的字符串) 依照空格進(jìn)行分割
PHP學(xué)習(xí)
$str = 'Hello PHP Java C# C++';
$result = explode(' ',$str);
print_r($result);
PHP學(xué)習(xí)字符串的連接
PHP學(xué)習(xí)
$str = 'Hello PHP Java C# C++';
//字符串的連接
$num = 100;
$str1 = $str.'<br/>Objective-C '.$num;
echo $str1;
echo '<br/>';
$str2 = "$str<br/>Objective-C $num"; //另一中輕便的寫法
echo $str2;
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP教程:分享10段PHP常用代碼》等實戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/8420.html