溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

10段PHP常用代碼編寫(xiě)方法教程

發(fā)布時(shí)間:2021-09-29 11:35:33 來(lái)源:億速云 閱讀:167 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“10段PHP常用代碼編寫(xiě)方法教程”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“10段PHP常用代碼編寫(xiě)方法教程”吧!

1、使用PHP Mail函數(shù)發(fā)送Email

$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); 
?﹥

2、PHP中的64位編碼和解碼

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;
}

3、獲取遠(yuǎn)程IP地址

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;
}

4、 日期格式化

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;
}

5、驗(yàn)證Email

$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.';
}

6、在PHP中輕松解析XML

//this is a sample xml string
$xml_string="﹤?xml version='1.0'?﹥
﹤moleculedb﹥
 ﹤molecule name='Benzine'﹥
 ﹤symbol﹥ben﹤/symbol﹥
 ﹤code﹥A﹤/code﹥
 ﹤/molecule﹥
 ﹤molecule name='Water'﹥
 ﹤symbol﹥h3o﹤/symbol﹥
 ﹤code﹥K﹤/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 /﹥';
}

7、數(shù)據(jù)庫(kù)連接

﹤?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".
 '﹤html﹥﹤head﹥'."n".
 '﹤title﹥404 Not Found﹤/title﹥'."n".
 '﹤/head﹥﹤body﹥'."n".
 '﹤h2﹥Not Found﹤/h2﹥'."n".
 '﹤p﹥The 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";
?﹥

8、創(chuàng)建和解析JSON數(shù)據(jù)

$json_data = array ('id'=﹥1,'name'=﹥"rolf",'country'=﹥'russia',
"office"=﹥array("google","oracle"));
echo json_encode($json_data);

9、處理MySQL時(shí)間戳

$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;
}

10、解壓縮Zip文件

﹤?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常用功能如下

1.PHP字符串

字符串聲明 變量=''或者""(一般情況會(huì)使用單引號(hào),因?yàn)閷?xiě)起來(lái)會(huì)比較方便)

$str = 'Hello PHP';
echo $str;

strpos 計(jì)算字符在字符串中的位置(從0開(kāi)始)

$str = 'Hello PHP';
echo strpos($str,'o');  //計(jì)算字符在字符串中的位置
echo '<br/>';
echo strpos($str,'PH');

substr 截取字符串

$str = 'Hello PHP';
//截取字符串
$str1 = substr($str,2,3); //從2位置開(kāi)始截取,截取長(zhǎng)度為3的字符串
echo $str1;

不傳入長(zhǎng)度參數(shù)的話,會(huì)從指定位置一直截取到字符串的末尾

str_split 分割字符串  固定長(zhǎng)度的分割(默認(rèn)長(zhǎng)度為1)

$str = 'Hello PHP';
//分割字符串
$result = str_split($str); //將結(jié)果保存到一個(gè)數(shù)組中
print_r($result); //使用print_r輸入一個(gè)數(shù)組
echo '<br/>';
$result1 = str_split($str,2);
print_r($result1);

explode(分割字符,待分割的字符串) 按照空格進(jìn)行分割

$str = 'Hello PHP Java C# C++';
$result = explode(' ',$str);
print_r($result);

字符串的連接

$str = 'Hello PHP Java C# C++';
//字符串的連接
$num = 100;
$str1 = $str.'<br/>Objective-C '.$num;
echo $str1;
echo '<br/>';
$str2 = "$str<br/>Objective-C $num"; //另一中簡(jiǎn)便的寫(xiě)法
echo $str2;

感謝各位的閱讀,以上就是“10段PHP常用代碼編寫(xiě)方法教程”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)10段PHP常用代碼編寫(xiě)方法教程這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI