北京时间:
 
 
 首页 
 
 
 
 
 
 
 洛杉矶论坛 
   
洛杉矶华人论坛 |  搜索  |  注册  |  登陆  |  FAQ 
 
 
比较不错的PHP函数

 
发表新帖   回复帖子    洛杉矶华人论坛 首页 -> 技术交流
阅读上一个主题 :: 阅读下一个主题  
留言
比较不错的PHP函数  引用并回复
 
发布人: Mrs LA   发布于: 2006/03/18, 7:45 pm

中文截取

GBK/GB2312:

CODE:

function cutstr($string, $length) {

$strcut = '';

if(strlen($string) > $length) {

for($i = 0; $i < $length - 3; $i++) {

$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];

}

return $strcut.' ...';

} else {

return $string;

}

}

UTF-8:

CODE

function cutstr($string, $length) {

$strcut= '';

if(strlen($string) > $length) {

preg_match_all("/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf]"

."[x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf]"

."[x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/", $string, $info);

for($i=0; $i

$strcut .= $info[0][$i];

$j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;

if ($j > $length - 3) {

return $strcut." ...";

}

}

return join('', $info[0]);

} else {

return $string;

}

}





_________________
支持洛杉矶华人,点评洛杉矶华人商家


最后进行编辑的是 Mrs LA on 2006/03/19, 6:10 pm, 总计第 3 次编辑
返回页首
阅览成员资料 (Profile) 发送私人留言 (PM)
这个函数可以用来判断函数的存在性  引用并回复
 
发布人: Mrs LA   发布于: 2006/03/18, 7:48 pm

bool function_exists ( string function_name )





_________________
支持洛杉矶华人,点评洛杉矶华人商家
返回页首
阅览成员资料 (Profile) 发送私人留言 (PM)
str_replace 这个比其他的语言里面的好用多了  引用并回复
 
发布人: Mrs LA   发布于: 2006/03/18, 7:54 pm

mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count] )

search : the string will be replaced

replace: the new string

subject: the orginal string

说句实话,名字叫得比较奇怪





_________________
支持洛杉矶华人,点评洛杉矶华人商家
返回页首
阅览成员资料 (Profile) 发送私人留言 (PM)
email 用在url里面显示  引用并回复
 
发布人: Mrs LA   发布于: 2006/03/18, 8:00 pm

function emailconv($email, $tolink = 1) {

$email = str_replace(array('@', '.'), array('@', '.'), $email);

return $tolink ? '<a href="mailto: '.$email.'">'.$email.'</a>': $email;

}





_________________
支持洛杉矶华人,点评洛杉矶华人商家
返回页首
阅览成员资料 (Profile) 发送私人留言 (PM)
随即函数  引用并回复
 
发布人: Mrs LA   发布于: 2006/03/18, 8:27 pm

People Finders

void mt_srand ( int seed )

用 seed 来给随机数发生器播种。

int mt_rand ( [int min, int max] )

产生随机数





_________________
支持洛杉矶华人,点评洛杉矶华人商家
返回页首
阅览成员资料 (Profile) 发送私人留言 (PM)
Iinsensitive string comparison  引用并回复
 
发布人: Mrs LA   发布于: 2006/03/18, 8:36 pm

int strcasecmp ( string str1, string str2 )

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.





_________________
支持洛杉矶华人,点评洛杉矶华人商家
返回页首
阅览成员资料 (Profile) 发送私人留言 (PM)
变量及常量的正则表达式  引用并回复
 
发布人: Mrs LA   发布于: 2006/03/18, 10:36 pm

$var_regexp = "((\$[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)([[a-zA-Z0-9_-."''[]$x7f-xff]+])*)";

$const_regexp = "([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)";





_________________
支持洛杉矶华人,点评洛杉矶华人商家
返回页首
阅览成员资料 (Profile) 发送私人留言 (PM)
随机字符串  引用并回复
 
发布人: Mrs LA   发布于: 2006/03/21, 9:12 pm

function gen_rand_string($hash)

{

$chars = array( 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0');

$max_chars = count($chars) - 1;

srand( (double) microtime()*1000000);

$rand_str = '';

for($i = 0; $i < 8; $i++)

{

$rand_str = ( $i == 0 ) ? $chars[rand(0, $max_chars)] : $rand_str . $chars[rand(0, $max_chars)];

}

return ( $hash ) ? md5($rand_str) : $rand_str;

}





_________________
支持洛杉矶华人,点评洛杉矶华人商家
返回页首
阅览成员资料 (Profile) 发送私人留言 (PM)
如何用php实现Redirect的功能  引用并回复
 
发布人: admin   发布于: 2006/04/13, 9:30 pm

header('location: http://www.chineseinla.com/index.php');




返回页首
阅览成员资料 (Profile) 发送私人留言 (PM)
从以前的帖子开始显示:   
点评这篇文章
 
1 2 3 4 5
0个人参与评分
发表新帖   回复帖子    洛杉矶华人论坛 首页 -> 技术交流 论坛时间为 PST (美国/加拿大)
1页/共1   
转跳到:  


 
Copyright 2006-2008 www.ChineseInLA.com All rights reserved. Privacy Policy