以下是十五个最有用的php代码片段。您可能还想对任何代码或者你也可以分享你的代码片段在评论部分如果你认为它可能对其他人有用。
1。发送邮件使用邮件函数在php
与站长百科 php教程同步首发
2。base64编码和解码字符串在php
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。在php中获得远程ip地址
function getremoteipaddress() {
$ip = $_server['remote_addr'];
return $ip;
}
上面的代码将无法工作,以防您的客户端代理服务器后面。在这种情况下使用函数来获得真正的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。秒到字符串
这个函数将返回给定时间段期间在天、小时、分钟和秒。
例如secstostr(1234567)将返回“14天6小时56分钟,7秒”
function secstostr($secs) {
if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.' day';if($days1){$r.='s';}if($secs>0){$r.=', ';}}
if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.' hour';if($hours1){$r.='s';}if($secs>0){$r.=', ';}}
if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.' minute';if($minutes1){$r.='s';}if($secs>0){$r.=', ';}}
$r.=$secs.' second';if($secs1){$r.='s';}
return $r;
}
5。电子邮件验证代码片段在php
$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。解析xml的简单的方法使用php
所需的扩展:simplexml
//this is a sample xml string
$xml_string=
ben
a
h2o
k
;
//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, '
';
}
7。数据库连接在php
8。创建和解析json数据在php
以下是php代码创建json数据格式,上面的示例使用的php数组。
$json_data = array ('id'=>1,'name'=>rolf,'country'=>'russia',office=>array(google,oracle));
echo json_encode($json_data);
下面的代码将解析json数据到php数组。
$json_string='{id:1,name:rolf,country:russia,office:[google,oracle]} ';
$obj=json_decode($json_string);
//print the parsed data
echo $obj->name; //displays rolf
echo $obj->office[0]; //displays google
9。mysql在php过程时间戳
$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。在php中生成一个身份验证代码
这个基本的代码片段将创建一个随机身份验证代码,或者仅仅是一个随机字符串。
11。在php的日期格式验证
验证一个日期在“yyyy mm dd”格式。
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;
}
12。http重定向在php
13。目录清单在php
14。在php浏览器检测脚本
15。zip文件解压缩一个
//use the code as following:
你怎么这样小的收集的php代码片段。你可能想要将您的代码片段在评论中与他人分享。
作者:ssoftware
http://www.bkjia.com/phpjc/478012.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/478012.htmltecharticle以下是十五个最有用的php代码片段。您可能还想对任何代码或者你也可以分享你的代码片段在评论部分如果你认为它可能对其他人有用。...