此代碼將創建一個可閱讀的字符串
/**************
*@length
**************/
function readable_random_string($length =
$conso=array("b"
"m"
$vocal=array("a"
$password="";
srand ((double)microtime()*
$max = $length/
for($i=
{
$password
$password
}
return $password;
}
如果不需要可閱讀的字符串
/*************
*@l
*/
function generate_rand($l){
$c= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
srand((double)microtime()*
for($i=
$rand
}
return $rand;
}
使用此代碼
function encode_email($email=
{
// remplazar aroba y puntos
$email = str_replace(
$email = str_replace(
$email = str_split($email
$linkText = str_replace(
$linkText = str_replace(
$linkText = str_split($linkText
$part
$part
$part
$part
$encoded =
$encoded
$encoded
foreach($email as $e)
{
$encoded
}
$encoded
foreach($linkText as $l)
{
$encoded
}
$encoded
$encoded
return $encoded;
}
電子郵件驗證也許是中最常用的網頁表單驗證
function is_valid_email($email
{
if(eregi("^([_a
if($test_mx)
{
list($username
return getmxrr($domain
}
else
return true;
else
return false;
}
function list_files($dir)
{
if(is_dir($dir))
{
if($handle = opendir($dir))
{
while(($file = readdir($handle)) !== false)
{
if($file != "
{
echo
}
}
closedir($handle);
}
}
}
刪除一個目錄
/*****
*@dir
*@virtual[optional]
*/
function destroyDir($dir
{
$ds = DIRECTORY_SEPARATOR;
$dir = $virtual ? realpath($dir) : $dir;
$dir = substr($dir
if (is_dir($dir) && $handle = opendir($dir))
{
while ($file = readdir($handle))
{
if ($file ==
{
continue;
}
elseif (is_dir($dir
{
destroyDir($dir
}
else
{
unlink($dir
}
}
closedir($handle);
rmdir($dir);
return true;
}
else
{
return false;
}
}
與大多數流行的 Web 服務如 twitter 通過開放 API 來提供數據一樣
$json_string=
$obj=json_decode($json_string);
echo $obj
echo $obj
//xml string
$xml_string="<?xml version=
<users>
<user id=
<name>Foo</name>
<email>foo@bar
</user>
<user id=
<name>Foobar</name>
<email>foobar@foo
</user>
</users>";
//load the xml string using simplexml
$xml = simplexml_load_string($xml_string);
//loop through the each node of user
foreach ($xml
{
//access attribute
echo $user[
//subnodes are accessed by
echo $user
echo $user
}
創建用戶友好的日志縮略名
function create_slug($string){
$slug=preg_replace(
return $slug;
}
該函數將獲取用戶的真實 IP 地址
function getRealIpAddr()
{
if (!emptyempty(實用PHP代碼二十一個實例
}
return $ip;
}
為用戶提供強制性的文件下載功能
/********************
*@file
*/
function force_download($file)
{
if ((isset($file))&&(file_exists($file))) {
header("Content
header(
header(
readfile("$file");
} else {
echo "No file selected";
}
}
function getCloud( $data = array()
{
$minimumCount = min( array_values( $data ) );
$maximumCount = max( array_values( $data ) );
$spread = $maximumCount
$cloudHTML =
$cloudTags = array();
$spread ==
foreach( $data as $tag => $count )
{
$size = $minFontSize + ( $count
* ( $maxFontSize
$cloudTags[] =
}
return join( "n"
}
/**************************
**** Sample usage ***/
$arr = Array(
echo getCloud($arr
PHP 提供了一個極少使用的 similar_text 函數
similar_text($string
//$percent will have the percentage of similarity
隨著 WordPress 越來越普及
/******************
*@email
*@size
*@default
*@rating
*/
function show_gravatar($email
{
echo
height="
}
所謂斷字 (word break)
// Original PHP code by Chirp Internet: www
// Please acknowledge use of this code by including this header
function myTruncate($string
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit)
return $string;
// is $break present between $limit and the end of the string?
if(false !== ($breakpoint = strpos($string
if($breakpoint < strlen($string)
$string = substr($string
}
}
return $string;
}
/***** Example ****/
$short_string=myTruncate($long_string
/* creates a compressed zip file */
function create_zip($files = array()
//if the zip file already exists and overwrite is false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip
return false;
}
//add the files
foreach($valid_files as $file) {
$zip
}
//debug
//echo
//close the zip
$zip
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
/***** Example Usage ***/
$files=array(
create_zip($files
/**********************
*@file
*@destination
*/
function unzip_file($file
// create object
$zip = new ZipArchive() ;
// open archive
if ($zip
die (’Could not open archive’);
}
// extract contents to destination directory
$zip
// close archive
$zip
echo
}
有時需要接受一些表單中的網址輸入
if (!preg_match("/^(http|ftp):/"
}
該函數將 URL 和 E
function makeClickableLinks($text) { $text = eregi_replace(
創建圖像縮略圖需要許多時間
/**********************
*@filename
*@tmpname
*@xmax
*@ymax
*/
function resize_image($filename
{
$ext = explode("
$ext = $ext[count($ext)
if($ext == "jpg" || $ext == "jpeg")
$im = imagecreatefromjpeg($tmpname);
elseif($ext == "png")
$im = imagecreatefrompng($tmpname);
elseif($ext == "gif")
$im = imagecreatefromgif($tmpname);
$x = imagesx($im);
$y = imagesy($im);
if($x <= $xmax && $y <= $ymax)
return $im;
if($x >= $y) {
$newx = $xmax;
$newy = $newx * $y / $x;
}
else {
$newy = $ymax;
$newx = $x / $y * $newy;
}
$im
imagecopyresized($im
return $im
}
From:http://tw.wingwit.com/Article/program/PHP/201311/20882.html