PHP常用函数
时间:2005-03-02 来源:周若水
常用函数,不用说的啦.........
function message($C_alert,$I_goback='') {
if(!empty($I_goback)) {
echo "alert('$C_alert');window.location.href='$I_goback';";
} else {
echo "alert('$C_alert');";
}
}
/**
* 判断下拉菜音的选取项
*
* 可以判断字符串一和字符串二是否相等.从而使相等的项目在下拉菜单中被选择
*
* @access public
* @param string $str1 要比较的字符串一
* @param string $str2 要比较的字符串二
* @return string 相等返回字符串"selected",否则返回空字符串
*/
function selected($str1,$str2) {
if($str1==$str2) {
return ' selected';
}
return '';
}
/**
* 截取中文部分字符串
*
* 截取指定字符串指定长度的函数,该函数可自动判定中英文,不会出现乱码
*
* @access public
* @param string $str 要处理的字符串
* @param int $strlen 要截取的长度默认为10
* @param string $other 是否要加上省略号,默认会加上
* @return string
*/
function showtitle($str,$strlen=10,$other=true) {
$j = 0;
for($i=0;$i0xa0) $j++;
if($j%2!=0) $strlen++;
$rstr=substr($str,0,$strlen);
if (strlen($str)>$strlen && $other) {$rstr.='...';}
return $rstr;
}
/////////////
function createdir($dir='')
{
if (!is_dir($dir))
{
$temp = explode('/',$dir);
$cur_dir = '';
for($i=0;$i $val) {
$string[$key] = dhtmlspecialchars($val);
}
} else {
$string = str_replace('&', '&', $string);
$string = str_replace('"', '"', $string);
$string = str_replace('', '>', $string);
$string = preg_replace('/&(#d{3,5};)/', '&\1', $string);
}
return $string;
}
/////////
function daddslashes($string, $force = 0) {
if(!$GLOBALS['magic_quotes_gpc'] || $force) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = daddslashes($val, $force);
}
} else {
$string = addslashes($string);
}
}
return $string;
}
/////////.
function random($length) {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i $length) {
if($sss){
$length=$length - 3;
$addstr=' ...';
}
for($i = 0; $i 127) {
$wordscut .= $string[$i].$string[$i + 1];
$i++;
} else {
$wordscut .= $string[$i];
}
}
return $wordscut.$addstr;
}
return $string;
}
/////////
function sizecount($filesize) {
if($filesize >= 1073741824) {
$filesize = round($filesize / 1073741824 * 100) / 100 . ' G';
} elseif($filesize >= 1048576) {
$filesize = round($filesize / 1048576 * 100) / 100 . ' M';
} elseif($filesize >= 1024) {
$filesize = round($filesize / 1024 * 100) / 100 . ' K';
} else {
$filesize = $filesize . ' bytes';
}
return $filesize;
}
////////////////
function gotourl($message='',$url='')
{
global $language;
$html ="";
if(!empty($url))
$html .="";
$html .="";
$html .="
";
$html .="";
$html .="";
$html .="";
$html .="";
$html .="".$language['messagebox_title']."";
$html .="";
$html .="
".$message."
";
if (!empty($url))
$html .="[".$language['messagebox_exp_1']."]";
else
$html .="[".$language['messagebox_exp_2']."]";
$html .="";
$html .="";
echo $html;
exit;
}
//////////////
function sqldumptable($table, $startfrom = 0, $currsize = 0) {
global $db, $multivol, $sizelimit, $startrow;
$offset = 64;
if(!$startfrom)
{
$tabledump = "DROP TABLE IF EXISTS $table;
";
$createtable = $db->query("SHOW CREATE TABLE $table");
$create = $db->fetch_row($createtable);
$tabledump .= $create[1].";
";
}
$tabledumped = 0;
$numrows = $offset;
while(($multivol && $currsize + strlen($tabledump) query("SELECT * FROM $table $limitadd");
$numfields = $db->num_fields($rows);
$numrows = $db->num_rows($rows);
while ($row = $db->fetch_row($rows))
{
$comma = "";
$tabledump .= "INSERT INTO $table VALUES(";
for($i = 0; $i < $numfields; $i++)
{
$tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
$comma = ",";
}
$tabledump .= ");
";
}
}
$startrow = $startfrom;
$tabledump .= "
";
return $tabledump;
}
//////////
function splitsql($sql){
$sql = str_replace("", "
", $sql);
$ret = array();
$num = 0;
$queriesarray = explode(";
", trim($sql));
unset($sql);
foreach($queriesarray as $query) {
$queries = explode("
", trim($query));
foreach($queries as $query) {
$ret[$num] .= $query[0] == "#" ? NULL : $query;
}
$num++;
}
return($ret);
}
function message($C_alert,$I_goback='') {
if(!empty($I_goback)) {
echo "alert('$C_alert');window.location.href='$I_goback';";
} else {
echo "alert('$C_alert');";
}
}
/**
* 判断下拉菜音的选取项
*
* 可以判断字符串一和字符串二是否相等.从而使相等的项目在下拉菜单中被选择
*
* @access public
* @param string $str1 要比较的字符串一
* @param string $str2 要比较的字符串二
* @return string 相等返回字符串"selected",否则返回空字符串
*/
function selected($str1,$str2) {
if($str1==$str2) {
return ' selected';
}
return '';
}
/**
* 截取中文部分字符串
*
* 截取指定字符串指定长度的函数,该函数可自动判定中英文,不会出现乱码
*
* @access public
* @param string $str 要处理的字符串
* @param int $strlen 要截取的长度默认为10
* @param string $other 是否要加上省略号,默认会加上
* @return string
*/
function showtitle($str,$strlen=10,$other=true) {
$j = 0;
for($i=0;$i0xa0) $j++;
if($j%2!=0) $strlen++;
$rstr=substr($str,0,$strlen);
if (strlen($str)>$strlen && $other) {$rstr.='...';}
return $rstr;
}
/////////////
function createdir($dir='')
{
if (!is_dir($dir))
{
$temp = explode('/',$dir);
$cur_dir = '';
for($i=0;$i $val) {
$string[$key] = dhtmlspecialchars($val);
}
} else {
$string = str_replace('&', '&', $string);
$string = str_replace('"', '"', $string);
$string = str_replace('', '>', $string);
$string = preg_replace('/&(#d{3,5};)/', '&\1', $string);
}
return $string;
}
/////////
function daddslashes($string, $force = 0) {
if(!$GLOBALS['magic_quotes_gpc'] || $force) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = daddslashes($val, $force);
}
} else {
$string = addslashes($string);
}
}
return $string;
}
/////////.
function random($length) {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i $length) {
if($sss){
$length=$length - 3;
$addstr=' ...';
}
for($i = 0; $i 127) {
$wordscut .= $string[$i].$string[$i + 1];
$i++;
} else {
$wordscut .= $string[$i];
}
}
return $wordscut.$addstr;
}
return $string;
}
/////////
function sizecount($filesize) {
if($filesize >= 1073741824) {
$filesize = round($filesize / 1073741824 * 100) / 100 . ' G';
} elseif($filesize >= 1048576) {
$filesize = round($filesize / 1048576 * 100) / 100 . ' M';
} elseif($filesize >= 1024) {
$filesize = round($filesize / 1024 * 100) / 100 . ' K';
} else {
$filesize = $filesize . ' bytes';
}
return $filesize;
}
////////////////
function gotourl($message='',$url='')
{
global $language;
$html ="";
if(!empty($url))
$html .="";
$html .="";
$html .="
";
$html .="";
$html .="";
$html .="";
$html .="";
$html .="".$language['messagebox_title']."";
$html .="";
$html .="
".$message."
";
if (!empty($url))
$html .="[".$language['messagebox_exp_1']."]";
else
$html .="[".$language['messagebox_exp_2']."]";
$html .="";
$html .="";
echo $html;
exit;
}
//////////////
function sqldumptable($table, $startfrom = 0, $currsize = 0) {
global $db, $multivol, $sizelimit, $startrow;
$offset = 64;
if(!$startfrom)
{
$tabledump = "DROP TABLE IF EXISTS $table;
";
$createtable = $db->query("SHOW CREATE TABLE $table");
$create = $db->fetch_row($createtable);
$tabledump .= $create[1].";
";
}
$tabledumped = 0;
$numrows = $offset;
while(($multivol && $currsize + strlen($tabledump) query("SELECT * FROM $table $limitadd");
$numfields = $db->num_fields($rows);
$numrows = $db->num_rows($rows);
while ($row = $db->fetch_row($rows))
{
$comma = "";
$tabledump .= "INSERT INTO $table VALUES(";
for($i = 0; $i < $numfields; $i++)
{
$tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
$comma = ",";
}
$tabledump .= ");
";
}
}
$startrow = $startfrom;
$tabledump .= "
";
return $tabledump;
}
//////////
function splitsql($sql){
$sql = str_replace("", "
", $sql);
$ret = array();
$num = 0;
$queriesarray = explode(";
", trim($sql));
unset($sql);
foreach($queriesarray as $query) {
$queries = explode("
", trim($query));
foreach($queries as $query) {
$ret[$num] .= $query[0] == "#" ? NULL : $query;
}
$num++;
}
return($ret);
}
相关阅读 更多 +