有时候显示的内容长度要限制一下,下面这个函数来自DZ,可以设定要截取自己指定长度的内容
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i]; 此处考虑到了用户操作DEL键,如果只是从数据库中取出内容显示的话,可以直接$strcut .= $string[$i]; 这样就可以了
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;
}
}
$string=’12345678901234567890′;
$length=10;
echo cutstr($string, $length);
此函数是DZ中的 类似PHP中的in_array函数的JS “in_array”,同样是检查一个值是否为数组元素,来是很好用的,具体如下:
<script>
function in_array(needle, haystack) {
if(typeof needle == ’string’ || typeof needle == ‘number’) {
for(var i in haystack) {
if(haystack[i] == needle) {
return true;
}
}
}
return false;
}
var arr = new Array(‘123′,’456′);
alert(in_array(‘789′,arr));
</script>
此函数的返回值为BOOL型
此函数是使用PHP中的implode函数自定义的,但是用起来要比implode要好用
/*
*数组转换
*/
function implodeids($array) {
if(!empty($array)) {
return “‘”.implode(“‘,’”, is_array($array) ? $array : array($array)).”‘”;
} else {
return ”;
}
}
function writetocache($script, $cachenames, $cachedata = ”, $prefix = ‘cache_’) {
global $authkey;
if(is_array($cachenames) && !$cachedata) {
foreach($cachenames as $name) {
$cachedata .= getcachearray($name, $script);
}
}
$dir = DISCUZ_ROOT.’./forumdata/cache/’;
if(!is_dir($dir)) {
@mkdir($dir, 0777);
}
if($fp = @fopen(“$dir$prefix$script.php”, ‘wb’)) {
fwrite($fp, “<?php\n//Discuz! cache file, DO NOT modify me!”.
”\n//Created: “.date(“M j, Y, G:i”).
”\n//Identify: “.md5($prefix.$script.’.php’.$cachedata.$authkey).”\n\n$cachedata?>”);
fclose($fp);
} else {
exit(‘Can not write to cache files, please check directory ./forumdata/ and ./forumdata/cache/ .’);
}
}
if($_REQUEST['operate'] == “js”){
$cacheFile = DISCUZ_ROOT . ‘./forumdata/cache/cache_counta.php’;
if (($time – @filemtime($cacheFile)) > $cacheTime) {
//查询当天评分总数
$ltime = date(‘H 点 i 分’,time());
$numRows = $db->query(“SELECT time FROM {$tablepre}grade WHERE time>=’$time0′ AND time<=’$time24′”);
$count = $db->num_rows($numRows);
$countbar = ‘$newcountbar = \’<a href=”plugin.php?identifier=counta&module=counta”>截至到’. $ltime .’,总共收到评分’. $count .’个, 点击请看详情</a>\’;’.”\n\n”;
writetocache(“counta”, ”, $countbar);
}
require ($cacheFile);
echo “document.write(‘$newcountbar’)”;
exit();
}
<script type=”text/javascript” src=”plugin.php?identifier=counta&module=counta&operate=js”></script>

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 