search

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("");
}

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.简述论坛中无限分类的实现原理。
答:

//指定分类id变量$category_id,然后返回该分类的所有子类
//$default_category为默认的选中的分类
function Get_Category($category_id = 0,$level = 0, $default_category = 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]] = 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;
}

?>

class cate
{

        function Get_Category($category_id = 0,$level = 0, $default_category = 0)
        {
             echo $category_id;
             $arr = array(
              '0' => 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 "";
?>

求两个日期的差数,例如2007-2-5 ~ 2007-3-6 的日期差数

 

 

方法一:

class Dtime

{

function get_days($date1, $date2)

{

   $time1 = strtotime($date1);

   $time2 = strtotime($date2);

   return ($time2-$time1)/86400;

}

}


$Dtime = new Dtime;

echo $Dtime->get_days('2007-2-5', '2007-3-6');

?>


方法二:

$temp = explode('-', '2007-2-5');

$time1 = mktime(0, 0, 0, $temp[1], $temp[2], $temp[0]);

$temp = explode('-', '2007-3-6');

$time2 = mktime(0, 0, 0, $temp[1], $temp[2], $temp[0]);

echo ($time2-$time1)/86400;

字符串“open_door” 转换成 “opendoor”、”make_by_id” 转换成 ”makebyid”

 


function changeStyle($str){
$arr = explode('_',$str);
$new_str = '';
foreach($arr as $v){
$new_str .= ucfirst($v);
}
return $new_str;
}
echo changeStyle('open_door')."
"; //OpenDoor
echo changeStyle('make_by_id'); //MakeById

 

要求写一段程序,实现以下数组$arr1转换成数组$arr2:

 

$arr1 = array (

 

       '0' => 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' )

 

)

 

);

 

实现代码如下:
$arr1 = array (

 

       '0' => 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 '

';<br>print_r($arr2);<br>echo '<pre class="brush:php;toolbar:false">
';
?>


 

 

//插入排序(一维数组)

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);




Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.