1、实现中文字串截取无乱码的方法。(3分)
答:function gbsubstr($string, $start, $length) {
if(strlen($string)>$length){
$str=null;
$len=$start+$length;
for($i=$start;$i if(ord(substr($string,$i,1))>0xa0){
$str.=substr($string,$i,2);
$i++;
}else{
$str.=substr($string,$i,1);
}
}
return $str.'...';
}else{
return $string;
}
}
2. 请写一个函数验证电子邮件的格式是否正确 (2分)
答:function checkemail($email)
{
$pregemail = /([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i;
return preg_match($pregemail,$email);
}
3、完成以下:
(一)创建新闻发布系统,表名为message有如下字段 (3分)
id 文章id
title 文章标题
content 文章内容
category_id 文章分类id
hits 点击量
答:create table 'message'(
'id' int(10) not null auto_increment,
'title' varchar(200) default null,
'content' text,
'category_id' int(10) not null,
'hits' int(20),
primary key('id');
)engine=innodb default charset=utf8;
(二)同样上述新闻发布系统:表comment记录用户回复内容,字段如下 (4分)
comment_id 回复id
id 文章id,关联message表中的id
comment_content 回复内容
现通过查询数据库需要得到以下格式的文章标题列表,并按照回复数量排序,回复最高的排在最前面
文章id 文章标题 点击量 回复数量
用一个sql语句完成上述查询,如果文章没有回复则回复数量显示为0
答:select message.id id,message.title title,if(message.`hits` is null,0,message.`hits`) hits,
if(comment.`id` is null,0,count(*)) number from message left join
comment on message.id=comment.id group by message.`id`;
(三)上述内容管理系统,表category保存分类信息,字段如下 (3分)
category_id int(4) not null auto_increment;
categroy_name varchar(40) not null;
用户输入文章时,通过选择下拉菜单选定文章分类
写出如何实现这个下拉菜单
答:function categorylist()
{
$result=mysql_query(select category_id,categroy_name from category)
or die(invalid query: . mysql_error());
print(\n);
while($rowarray=mysql_fetch_array($result))
{
print(.$rowarray['categroy_name'].\n);
}
print();
}
4. 写一个函数,算出两个文件的相对路径
如 $a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/c.php';
计算出 $b 相对于 $a 的相对路径应该是 http://www.cnblogs.com/c/d将()添上
答:function getrelativepath($a, $b) {
$returnpath = array(dirname($b));
$arra = explode('/', $a);
$arrb = explode('/', $returnpath[0]);
for ($n = 1, $len = count($arrb); $n if ($arra[$n] != $arrb[$n]) {
break;
}
}
if ($len - $n > 0) {
$returnpath = array_merge($returnpath, array_fill(1, $len - $n, '..'));
}
$returnpath = array_merge($returnpath, array_slice($arra, $n));
return implode('/', $returnpath);
}
echo getrelativepath($a, $b);
5.写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。
答:
function my_scandir($dir)
{
$files = array();
if ( $handle = opendir($dir) ) {
while ( ($file = readdir($handle)) !== false ) {
if ( $file != .. && $file != . ) {
if ( is_dir($dir . / . $file) ) {
$files[$file] = scandir($dir . / . $file);
}else {
$files[] = $file;
}
}
}
closedir($handle);
return $files;
}
}
6.简述论坛中无限分类的实现原理。
答:
query( $sql );
while ($rows = $db->fetch_array($result))
{
$category_array[$rows[categoryparentid]][$rows[categoryid]] = array('id' => $rows[categoryid], 'parent' => $rows[categoryparentid], 'name' => $rows
[categoryname]);
}
if (!isset($category_array[$category_id]))
{
return ;
}
foreach($category_array[$category_id] as $key => $category)
{
if ($category['id'] == $default_category)
{
echo }else
{
echo }
if ($level > 0)
{
echo > . str_repeat( , $level ) . . $category['name'] . \n;
}
else
{
echo > . $category['name'] . \n;
}
get_category($key, $level + 1, $default_category);
}
unset($category_array[$category_id]);
}
//指定分类id,然后返回数组
function category_array($category_id = 0,$level=0)
{
global $db;
$sql = select * from category order by categoryid desc;
$result = $db->query($sql);
while ($rows = $db->fetch_array($result))
{
$category_array[$rows['categoryparentid']][$rows['categoryid']] = $rows;
}
foreach ($category_array as $key=>$val)
{
if ($key == $category_id)
{
foreach ($val as $k=> $v)
{
$options[$k] =
array(
'id' => $v['categoryid'], 'name' => $v['categoryname'], 'level' => $level, 'parentid'=>$v['categoryparentid']
);
$children = category_array($k, $level+1);
if (count($children) > 0)
{
$options = $options + $children;
}
}
}
}
unset($category_array[$category_id]);
return $options;
}
?>
array(
'1' => array('id' => 1, 'parent' => 0, 'name' => '1111'),
'2' => array('id' => 2, 'parent' => 0, 'name' => '2222'),
'4' => array('id' => 4, 'parent' => 0, 'name' => '4444')
),
'1' => array(
'3' => array('id' => 3, 'parent' => 1, 'name' => '333333'),
'5' => array('id' => 5, 'parent' => 1, 'name' => '555555')
),
'3' => array(
'6' => array('id' => 6, 'parent' => 3, 'name' => '66666'),
'7' => array('id' => 7, 'parent' => 3, 'name' => '77777')
),
'4' => array(
'8' => array('id' => 8, 'parent' => 4, 'name' => '8888'),
'9' => array('id' => 9, 'parent' => 4, 'name' => '9999')
)
);
if (!isset($arr[$category_id]))
{
return ;
}
foreach($arr[$category_id] as $key => $cate)
{
if ($cate['id'] == $default_category)
{
$txt = }else{
$txt = }
if ($level > 0)
{
$txt1 = > . str_repeat( -, $level ) . . $cate['name'] . \n;
}else{
$txt1 = > . $cate['name'] . \n;
}
$val = $txt.$txt1;
echo $val;
self::get_category($key, $level + 1, $default_category);
}
}
function getflush($category_id = 0,$level = 0, $default_category = 0)
{
ob_start();
self::get_category($category_id ,$level, $default_category);
$out = ob_get_contents();
ob_end_clean();
return $out;
}
}
$id =$_get['id'];
echo ;
$c = new cate();
//$c->get_category();
$ttt= $c->getflush($id,'0','3');
echo $ttt;
echo ;
?>
求两个日期的差数,例如2007-2-5 ~ 2007-3-6 的日期差数
方法一:
get_days('2007-2-5', '2007-3-6');
?>
方法二:
array ('fid' => 1, 'tid' => 1, 'name' =>'name1' ),
'1' => array ('fid' => 1, 'tid' => 2 , 'name' =>'name2' ),
'2' => array ('fid' => 1, 'tid' => 5 , 'name' =>'name3' ),
'3' => array ('fid' => 1, 'tid' => 7 , 'name' =>'name4' ),
'4' => array ('fid' => 3, 'tid' => 9, 'name' =>'name5' )
);
$arr2 = array (
'0' => array (
'0' => array ( 'tid' => 1, 'name' => 'name1'),
'1' => array ( 'tid' => 2, 'name' => 'name2'),
'2' => array ( 'tid' => 5, 'name' => 'name3'),
'3' => array ( 'tid' => 7, 'name' => 'name4')
),
'1' => array (
'0' => array ( 'tid' => 9, 'name' => 'name5' )
)
);
实现代码如下:
array ('fid' => 1, 'tid' => 1, 'name' =>'name1' ),
'1' => array ('fid' => 1, 'tid' => 2 , 'name' =>'name2' ),
'2' => array ('fid' => 1, 'tid' => 5 , 'name' =>'name3' ),
'3' => array ('fid' => 1, 'tid' => 7 , 'name' =>'name4' ),
'4' => array ('fid' => 3, 'tid' => 9, 'name' =>'name5' )
);
$arr2 = array();
$i = 0;
foreach($arr1 as $key=>$val)
{
unset($val['fid']); //将键值为fid的元素释放(去掉)
$arr2[$i][] = $val;
if($key == 3)
$i++;
}
echo '
';
print_r($arr2);
echo '
';
?>
//插入排序(一维数组)
function insert_sort($arr){
$count = count($arr);
for($i=1; $i
$tmp = $arr[$i];
$j = $i - 1;
while($arr[$j] > $tmp){
$arr[$j+1] = $arr[$j];
$arr[$j] = $tmp;
$j--;
}
return $arr;
//选择排序(一维数组)
function select_sort($arr){
$count = count($arr);
for($i=0; $i
$k = $i;
for($j=$i+1; $j
if ($arr[$k] > $arr[$j])
$k = $j;
if ($k != $i){
$tmp = $arr[$i];
$arr[$i] = $arr[$k];
$arr[$k] = $tmp;
}
}
return $arr;
//冒泡排序(一维数组)
function bubble_sort($array){
$count = count($array);
if ($count
for($i=0; $i
for($j=$count-1; $j>$i; $j--){
if ($array[$j]
$tmp = $array[$j];
$array[$j] = $array[$j-1];
$array[$j-1] = $tmp;
}
}
return $array;
//快速排序(一维数组)
function quick_sort($array){
if (count($array)
$key = $array[0];
$left_arr = array();
$right_arr = array();
for ($i=1; $i if ($array[$i]
$left_arr[] = $array[$i];
else
$right_arr[] = $array[$i];
$left_arr = quick_sort($left_arr);
$right_arr = quick_sort($right_arr);
return array_merge($left_arr, array($key), $right_arr);