必须收藏的php实用代码片段
在编写代码的时候有个神奇的工具总是好的!下面这里收集了 40+ PHP 代码片段,可以帮助你开发PHP 项目。 之前已经为大家分享了《必须收藏的23个php实用代码片段》。 这些PHP 片段对于PHP 初学者也非常有帮助,非常容易学习,让我们开始学习吧~ 24. 从 PHP 数据创建 CSV 文件 语法: 25. 解析 XML 数据 A
K
//load the xml string using simplexml function //loop through the each node of molecule 26. 解析 JSON 数据 name; //displays rolf
echo $obj->office[0]; //displays google
27. 获取当前页面 URL 这个 PHP 片段可以帮助你让用户登录后直接跳转到之前浏览的页面 语法: 28. 从任意的 Twitter 账号获取最新的 Tweet children() as $child) {
foreach ($child as $value) {
if($value->getName() == "link") $link = $value['href'];
if($value->getName() == "content") {
$content = $value . "";
echo '
'; } } } } 语法: 29. 转发数量 使用这个 PHP 片段可以检测你的页面 URL 有多少转发数量 story->url_count;
if($retweets){
return $retweets;
} else {
return 0;
}
}
语法: 30. 计算两个日期的差 diff($date2);
echo "difference " . $interval->y . " years," . $interval->m." months,".$interval->d." days ";
// shows the total amount of days (not divided into years,months and days like above)
echo "difference " . $interval->days . " days ";
-------------------------------------------------------- OR
/**
31. 删除文件夹内容 语法: 32. 搜索和高亮字符串中的关键字 $1",$text );
}
return $text;
}
语法: 33. 写入文件 34. 根据 URL 下载图片 语法: 35. 检测 URL 是否有效 语法: 36. 生成二维码 "http://","TEL" => "TEL:","TXT"=>"","EMAIL" => "MAILTO:");
if(!in_array($type,array("URL","TEL","TXT","EMAIL")))
{
$type = "TXT";
}
if (!preg_match('/^'.$types[$type].'/',$data))
{
$data = str_replace("","",$types[$type]).$data;
}
$ch = curl_init();
$data = urlencode($data);
curl_setopt($ch,CURLOPT_URL,'http://chart.apis.google.com/chart');
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,'chs='.$size.'x'.$size.'&cht=qr&chld='.$ec.'|'.$margin.'&chl='.$data);
curl_setopt($ch,false);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
语法: 37. 计算两个地图坐标之间的距离 语法: 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).'';
}
?>
38. 获取一个特定话题标签的所有 Tweets Connecting to
$url..."; $ch = curl_init($url); curl_setopt ($ch,TRUE); $xml = curl_exec ($ch); curl_close ($ch); //If you want to see the response from Twitter,uncomment this next part out: //echo "Response: "; //echo "".htmlspecialchars($xml).""; $affected = 0; $twelement = new SimpleXMLElement($xml); foreach ($twelement->entry as $entry) { $text = trim($entry->title); $author = trim($entry->author->name); $time = strtotime($entry->published); $id = $entry->id; echo " Tweet from ".$author.": ".$text."Posted ".date('n/j/y g:i a',$time).""; } return true ; }39. 添加 th,st,nd 或者 rd 作为数字的后缀 4) ? 'th'
: (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1)
? 'th' : 'st' : 'nd' : 'rd' : 'th'));
return $cdnl.$ext;
}
语法: 40. 限制文件下载的速度 // set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5; if(file_exists($local_file) && is_file($local_file)) { // send headers header('Cache-control: private'); header('Content-Type: application/octet-stream'); header('Content-Length: '.filesize($local_file)); header('Content-Disposition: filename='.$download_file); // flush content
} // close file stream 41. 把文本转换成图片 42. 获取远程文件的大小 语法 43. 使用 imagebrick 进行 pdf 到图像的转换 44. 使用 tinyurl 生成短网址 语法: 45. youtube 下载链接生成器 使用下面的 PHP 片段可以让你的用户下载 Youtube 视频 save as):ntt";
echo "ntt";
echo "ntt";
echo "ntt";
echo "n";
}
46. Facebook 样式的时间戳 $periods = array("second","minute","hour","day","week","month","year","decade");
$lengths = array("60","60","24","7","4.35","12","10"); $now = time();
if(empty($unix_date)) { } else { for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { $difference = round($difference); if($difference != 1) { return "$difference $periods[$j] {$tense}"; 语法: 以上就是本文的全部内容,希望对大家的学习有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |