1. 程式人生 > >Php的小練習

Php的小練習

1.獲取一個檔案的字尾名? 456.html.txt.jPeG
程式碼如下:

//擷取檔名方法一
$str="456.html.txt.jpEG";
  preg_match_all('/.[a-zA-Z0-9]+/',$str,$coutArry);
//   var_dump(count($coutArry[0]));
  echo $coutArry[0][3];
//擷取檔名方法二
 echo "<br>";
$filenum=strrpos($str,".");
 echo substr($str, $filenum);

2.實現簡單的0-9純數字加密

程式碼如下:

$str = '201610234587923'
; function jiami($str){ $str = (string)$str; $arr = array('t','y','d','e','r','w','b','h','x','p'); $return = ''; for ($i=0;$i<strlen($str);$i++){ $temp = $str[$i];//2 $to_temp = $arr[$temp];//u $return .= $to_temp; } return $return; } function jiemi($str
)
{
$arr = array('t','y','d','e','r','w','b','h','x','p'); $return = ''; for ($i=0;$i<strlen($str);$i++){ $char = $str[$i]; $num = array_search($char, $arr); $return .= $num; } return $return; } var_dump($str); $s = jiami($str);//uewcxrerebby,uewcxrerebby var_dump($s
); $s1 = jiemi($s); var_dump($s1);

3.6位隨機驗證碼圖片生成

<?php
header("Content-type: image/png");
function yangzheng(){
$imgmap=imagecreatetruecolor(150, 60);
$imgcolor=imagecolorallocate($imgmap,255, 255, 255);
$stringArray=array('A','B','C','D','k','p','U','y','z','1','2','3','4','5','6','7','8','9');
// $stringArray=array('你','好','英','魚','哈','逗','喵','貓');
imagefill($imgmap, 0,0, $imgcolor);
for($k=0;$k<4;$k++){

   $Color=imagecolorallocate($imgmap, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
   imageline($imgmap,mt_rand(0, 150), mt_rand(0,60), mt_rand(0, 150), mt_rand(0,60), $Color);
   imagefttext($imgmap,25,mt_rand(0, 10), 10+$k*30,mt_rand(20, 40)+10, $Color,'font/li.TTF', $stringArray[mt_rand(0, count($stringArray)-1)]);
}
for($i=0;$i<1000;$i++){
    $colorPoint=imagecolorallocate($imgmap, mt_rand(0, 255),mt_rand(0, 255), mt_rand(0, 255));
   imagesetpixel($imgmap, mt_rand(0, 150), mt_rand(0, 60),  $colorPoint);
}
imagepng($imgmap);
}
yangzheng();

4.將image資料夾中的4張圖片縮略成200*200的圖片

程式碼如下:

<?php
header("Content-type: image/png");
//$x需要裁剪的寬,$y需要裁剪的高,$fillname需要處理的檔案
function tailor($x,$y,$filename){
    $original = getimagesize($filename);
    $type = $original['mime'];
    $bili = 0;
    switch ($type){
        case 'image/jpeg':
        case 'image/jpg':
            $fun_name = 'imagecreatefromjpeg';
            break;
        case 'image/png':
            $fun_name = 'imagecreatefrompng';
            break;
        case 'image/gif':
            $fun_name = 'imagecreatefromgif';
            break;
    }
    $oldImg=$fun_name($filename);
if ($original[0] > $original[1]){
    $bili = $original[0]/$x;
}else{
    $bili = $original[1]/$y;
}
if ($bili>1){
   $newWidth = $original[0]/$bili;
   $newHeight = $original[1]/$bili;
}else{
    $newWidth=$original[0];
    $newHeight = $original[1];
}
   $newImg = imagecreatetruecolor($newWidth, $newHeight);
   imagecopyresized($newImg, $oldImg, 0, 0, 0, 0, $newWidth, $newHeight,$original[0], $original[1]);
   imagepng($newImg);
}
//    imagejpeg($newImg);
tailor(100, 100,'imgs/8-8.jpg' );

5.九九乘法表

程式碼如下:

//久久乘法表
for($i = 1;$i<= 9;$i++){
    for ($j = 1;$j<=$i;$j++){
        $value = $i*$j;
        echo $j.'*'.$i.'='.$value.'  ';

    }
    echo '<br>';
}

6.水仙花數
程式碼如下:

for($i = 0;$i<10;$i++){
    for($j = 0;$j<10;$j++){
        for ($k = 0;$k<10;$k++){
            $vaules = pow($i, 3)+pow($j, 3)+pow($k, 3);
          if($vaules == $i*100+$j*10+$k && $vaules != 1 && $vaules != 0){
              var_dump($vaules);
          }
        }
    }
}

7.封裝一個求平均值的函式,引數形式為array(6,7,8…)或者’6,7,8…’

程式碼如下:

function avg($ars){
    $sum=0;
    if(!is_array($ars)&&!is_string($ars))
    {
        return false;
    }elseif(is_array($ars))
      {
       foreach ($ars as $p)
       {
           $sum += $p;
       }
       $avg=$sum/count($ars);
    }else{
$ars2=explode(',', $ars);
    foreach ($ars2 as $k)
     {
         $sum += $k;
      }
     $avg=$sum/count($ars2);
       }
    return  round($avg,2);
}
$s=avg(1);
 $ss=avg(array(1,2,3));
 $sss=avg('6,7,1');
 var_dump($sss);
 var_dump($ss);
var_dump($s);

8.計算 公雞5塊一隻,母雞3塊一隻,小雞1塊錢3只 100塊錢買100只雞,問公雞,母雞,小雞各多少隻?


    for($x = 0;$x<100;$x++){
        for($y = 0;$y<=(100-$x);$y++){
            for($z = 0;$z<=(100-$x-$y);$z++){
                if(((5*$x+3*$y+$z/3) == 100)&&(($x+$y+$z)==100))
                {
                    echo '公雞'.$x;
                    echo '母雞'.$y;
                    echo '小雞'.$z.'<br/>';
                }
            }
        }
    }

9.將字串的特殊符號轉換為相應的實體

程式碼如下:

$string = <<<CDATA
A>B
B<A
Tom&John
He said:"I'm fire."
CDATA;
$string2=htmlspecialchars($string);
var_dump($string2);

10.獲取多級檔案目錄:

//多級檔案目錄的獲取
function getDir($path){
    //echo $path."<br/>";
    $arr2[] = $path;
    if(is_file($path)){

    }else{
        $arr = scandir($path);
        foreach ($arr as $value){
            if($value !="." && $value !='..'){
                $arr1 = getDir($path."/".$value);//a/b/e
                $arr2 = array_merge($arr2,$arr1);
            }
        }
    }
    return $arr2;
}
$data = getDir('a');
var_dump($data);

11.刪除多集檔案目錄?
程式碼如下:

function delFile($path) {
    $arr = scandir($path);
//     var_dump($arr);
//判斷資料夾是否為空,是的話刪除
  if (count($arr)==2){
      rmdir($path);
  }else
      {
      foreach ($arr as $value)
      {
         $newPass=$path.'/'.$value;
     if(is_file($newPass) && $value!='.' && $value!= '..')
     {
              unlink($newPass);
      }
     if(is_dir($newPass)&& $value !='.' && $value != '..' )
     {
              $arr2 = scandir($newPass);
     if (count($arr2)!=2)
      {
         delFile($newPass);
      }else
           {
            rmdir($newPass);
             }
          }
      }
  }
  rmdir($path);
}
delFile('a');

12.字串的反轉:

程式碼如下:

function reverse($str){
    $len = strlen($str);
    $preg = '';
    $var = '';
    for($i=1;$i<=$len;$i++){
        $preg .= '(\w)';
        $var = '$'.$i.$var;
    }
    $temp = preg_replace("/^{$preg}$/", $var, $str);
    return $temp;
}
$str = 'abcd1234werwrwrwe';
$str1 = reverse($str);
var_dump($str,$str1);
$str2 = 'abc大1家2好';
preg_match_all('/[\x{4e00}-\x{9fa5}]+/u', $str2,$arr);
var_dump($arr);