php自动给文章加关键词链接的函数代码,PHP加密解密类
分类:微服架构

复制代码 代码如下:

1、实现中文字串截取无乱码的方法。(3分)
答:function GBsubstr($string, $start, $length) {
    if(strlen($string)>$length){
     $str=null;
     $len=$start+$length;
     for($i=$start;$i<$len;$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;
   }
}

<?php

$link = array(
'百度,',
'服务器软件,',
'脚本之家,',
);
$str = '在百度中搜索服务器软件就可以到脚本之家提供的软件

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

function i_array_column($input, $columnKey, $indexKey=null){
    if(!function_exists('array_column')){
        $columnKeyIsNumber  = (is_numeric($columnKey))?true:false;
        $indexKeyIsNull            = (is_null($indexKey))?true :false;
        $indexKeyIsNumber     = (is_numeric($indexKey))?true:false;
        $result                         = array();
        foreach((array)$input as $key=>$row){
            if($columnKeyIsNumber){
                $tmp= array_slice($row, $columnKey, 1);
                $tmp= (is_array($tmp) && !empty($tmp))?current($tmp):null;
            }else{
                $tmp= isset($row[$columnKey])?$row[$columnKey]:null;
            }
            if(!$indexKeyIsNull){
                if($indexKeyIsNumber){
                    $key = array_slice($row, $indexKey, 1);
                    $key = (is_array($key) && !empty($key))?current($key):null;
                    $key = is_null($key)?0:$key;
                }else{
                    $key = isset($row[$indexKey])?$row[$indexKey]:0;
                }
            }
            $result[$key] = $tmp;
        }
        return $result;
    }else{
        return array_column($input, $columnKey, $indexKey);
    }
}

夏季不再穿长筒袜近年成为一种时尚,甚至到了秋天也不例外。

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;

function randcode($len, $mode = 2){
    $rcode = '';
    switch($mode){
        case 1: //去除0、o、O、l等易混淆字符
            $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghijkmnpqrstuvwxyz';
            break;
        case 2: //纯数字
            $chars = '0123456789';
            break;
        case 3: //全数字+大小写字母
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
            break;
        case 4: //全数字+大小写字母+一些特殊字符
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz~!@#$%^&*()';
            break;
    }

专家提出,其实这种时尚不利于美腿。据悉,久坐久站缺少运动等原因易导致静脉曲张,尤其是长期站立工作的人,容易引起下肢静脉本身扩张、延长或静脉瓣膜损坏以至发病。具体表现为腿部肿胀,下肢静脉犹如蚯蚓状弯曲或结节成团,皮肤发紫,特别是踝和小腿内侧更为严重。

    (二)同样上述新闻发布系统:表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`;

    $count = strlen($chars) - 1;
    mt_srand((double)microtime() * 1000000);
    for($i = 0; $i < $len; $i++) {
        $rcode .= $chars[mt_rand(0, $count)];
    }

专家指出,长期站立工作的人或多或少都有一些下肢静脉曲张的表现,只是轻重程度不一样。下肢静脉曲张是一种治疗起来比较麻烦的疾病,严重的需要手术,所以最好的方法还是防患于未然,在症状尚不明显的时候,就采取防范措施,穿长筒弹力袜就是一种很好的方法,可帮助血液进入较大且较深处的静脉。

  (三)上述内容管理系统,表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("<select name='category' value=''>n");
    while($rowArray=mysql_fetch_array($result))
    {
       print("<option value='".$rowArray['category_id']."'>".$rowArray['categroy_name']."</option>n");
    }
    print("</select>");
}

    return $rcode;
}

保持理想体重,可减少静脉曲张的机会;避免紧身衣物,穿医用弹力袜,以免使血液聚积在腿部;小心食用避孕药,某些避孕药可能引起这种问题。';
$out=keylink($str,$link,1); //$str 原始字符 $link,替换链接数组, 3替换次数
echo $out;
function _sortDesc($a, $b) {
return (strlen($a[0]) < strlen($b[0])) ? 1 : -1;
}
function keylink($str,$link,$count=1)
{
$linkDefs = $link;
$linkMap = array();
foreach($linkDefs as $row) {
$linkMap[] = explode(',', $row);
}

4. 写一个函数,算出两个文件的相对路径

/**
 * $string 明文或密文
 * $operation 加密ENCODE或解密DECODE
 * $key 密钥
 * $expiry 密钥有效期
 */
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
    $ckey_length = 4;
    $key = md5($key);
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

foreach($linkMap as $row) {
$str = preg_replace('/(s*)('.$row[0].')(s*)/sui', '${2}', $str);
}

  如 $a = '/a/b/c/d/e.php';
  $b = '/a/b/12/34/c.php';
  计算出 $b 相对于 $a 的相对路径应该是
答:function getRelativePath($a, $b) {   
    $returnPath = array(dirname($b));   
    $arrA = explode('/', $a);   
    $arrB = explode('/', $returnPath[0]);   
    for ($n = 1, $len = count($arrB); $n < $len; $n++) {   
        if ($arrA[$n] != $arrB[$n]) {   
            break;   
        }    
    }   
    if ($len - $n > 0) {   
        $returnPath = array_merge($returnPath, array_fill(1, $len - $n, '..'));   
    }   

    $cryptkey = $keya.md5($keya.$keyc);
    $key_length = strlen($cryptkey);

usort($linkMap, '_sortDesc');

       
    $returnPath = array_merge($returnPath, array_slice($arrA, $n));   
    return implode('/', $returnPath);   
   }   
   echo getRelativePath($a, $b); 

    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
    $string_length = strlen($string);

$tmpKwds = array();

5.写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。

    $result = '';
    $box = range(0, 255);

foreach($linkMap as $i=>$row) {
list($kwd, $url) = $row;
for($j=$i+1; $j$subKwd = $linkMap[$j][0];
//如果包含其他关键字,暂时替换成其他字符串
if(strpos($kwd, $subKwd) !== false) {
$tmpKwd = '{'.md5($subKwd).'}';
$kwd = str_replace($subKwd, $tmpKwd, $kwd);
$tmpKwds[$tmpKwd] = $subKwd;
}
}
//把文字替换成链接
$str = preg_replace('/('.$row[0].')/sui', ''.$kwd.'', $str, $count);
}

答:
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;
     }
}

    $rndkey = array();
    for($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }

//把代替子关键字的字符串替换回来
foreach($tmpKwds as $tmp=>$kwd) {
$str = str_replace($tmp, $kwd, $str);
}
return $str;
}
?>

6.简述论坛中无限分类的实现原理。
答:
<?php

    for($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }

//指定分类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

    for($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }

[categoryName]);
 }
 if (!isset($category_array[$category_id]))
 {
 return "";
 }
 foreach($category_array[$category_id] AS $key => $category)
 { 
 if ($category['id'] == $default_category)
 {
 echo "<option selected value=".$category['id']."";
 }else
 {
 echo "<option value=".$category['id']."";
 }

    if($operation == 'DECODE') {
        if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        return $keyc.str_replace('=', '', base64_encode($result));
    }
}

 if ($level > 0)
 {
 echo ">" . str_repeat( " ", $level ) . " " . $category['name'] . "</option>n";
 }
 else
 {
 echo ">" . $category['name'] . "</option>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;
}

?>

<?php

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 = "<option selected value=".$cate['id']."";
                 }else{
                     $txt = "<option value=".$cate['id']."";
                 }
            
                 if ($level > 0)
                 {
                    $txt1 = ">" . str_repeat( "-", $level ) . " " . $cate['name'] . "</option>n";
                 }else{
                     $txt1 = ">" . $cate['name'] . "</option>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 "<select>";
$c = new cate();
//$c->Get_Category();
$ttt=  $c->getFlush($id,'0','3');
echo $ttt;
echo "</select>";
?>

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

 

 

方法一:

<?php

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

?>

方法二:

<?php

$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')."<br />"; //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' )

 

)

 

);

 

实现代码如下:
<?php
$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 '<pre>';
print_r($arr2);
echo '<pre />';
?>

 

排序

 

<?

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

function insert_sort($arr){

 $count = count($arr);

 for($i=1; $i<$count; $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<$count; $i++){

  $k = $i;

  for($j=$i+1; $j<$count; $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 <= 0) return false;

 

 for($i=0; $i<$count; $i++){

  for($j=$count-1; $j>$i; $j--){

   if ($array[$j] < $array[$j-1]){

    $tmp = $array[$j];

    $array[$j] = $array[$j-1];

    $array[$j-1] = $tmp;

   }

  }

 }

 return $array;

}

  

//快速排序(一维数组)

function quick_sort($array){

 if (count($array) <= 1) return $array;

  

 $key = $array[0];

 $left_arr = array();

 $right_arr = array();

 for ($i=1; $i<count($array); $i++){

  if ($array[$i] <= $key)

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

}

  

?>

本文由10bet手机官网发布于微服架构,转载请注明出处:php自动给文章加关键词链接的函数代码,PHP加密解密类

上一篇:进程超过了配置的活动超时时限的问题,的问题_php技巧_脚本之家 下一篇:没有了
猜你喜欢
热门排行
精彩图文