首页 > 编程技术 > php

php explode 与 implode用法与区别

发布时间:2016-11-25 15:13

下面这个函数是一个读取指定文件的内容然后再用php explode 与 implode来进行分析,然后我们再讲一下explode与implode的区别与使用方法

 function htmlTemplate($tag){
  $url ="file.txt";//file文件里面保存的是我要所数据并且是以<!-- 内容 -->为界线的。
  $content = file_get_contents($url); 读取文件
  if( !empty( $content ) ){
   $array = explode('<!-- 内容 -->',$content);  //进行数据处理,然后保存到数组array里面
  }else{
   echo 'error';
  }
  if( $tag ){
   return $array[1];//数据1里面是保存了一段数据
  }
  $temp = $array[16];
  $tempArray = explode('</span>',$temp); //16里面有数据</span>所以我们再进行inplode对数据进行合并成字符串就OK了。
  for($i = 0 ;$i<=3;$i++) {
   $tArray[] =$tempArray[$i];
  }
  return implode("</span>",$tArray ).'</span>';
  
 }

本站原创转载注明:www.111cn.net/phper/php.html

这是一款我自己没事时写的一款简单的把php文件生成html静态页面的程序代码哦,就是用str_replace来替换函数返回的数据就OK了。

$fileName = md5_filename($rs[0],$rs['z_a'],$rs['filesendid']);
   $path = get_path($rs['sysa']).$rs['z_a'].'/';
     
   $parent = $_SERVER["DOCUMENT_ROOT"];//substr(getcwd(),0,strrpos(getcwd(),'\'));  
    
   $filePath = $parent.'\'.$path;
   $tempLate = str_replace('{path}',path($rs['z_a'],$rs['sysa']),$fileContent);
   $tempLate = str_replace('{htmlAbout}',htmlAbout($rs['z_a'],10,8,0,''),$tempLate);   
   $tempLate = str_replace('{htmlTemplate}',htmlTemplate(0),$tempLate);
   $tempLate = str_replace('{htmlMenu}',htmlMenu($rs['sysa']),$tempLate);
   $tempLate = str_replace('{htmlHelp}',htmlHelp($rs['sysa']),$tempLate);
   $tempLate = str_replace('{htmlNews}',htmlAbout($rs['z_a'],0,8,0,''),$tempLate);
   $tempLate = str_replace('{htmlHot}',htmlAbout($rs['z_a'],0,8,1,''),$tempLate);
   $tempLate = str_replace('{htmlSoure}',htmlTemplate(1),$tempLate);
   $tempLate = str_replace('php简单生成html静态页面代码',$rs['title'],$tempLate);
   $tempLate = str_replace('{htmlContent}',$rs['content'],$tempLate);
   $tempLate = str_replace('{description}',$rs['description'],$tempLate);
   $tempLate = str_replace('{keywords}',$rs['keywords'],$tempLate);
   makeHtml($tempLate,$filePath,$fileName);

function makeHtml($msgContent,$filePath,$fileName){//生成html文件  
  if(!is_dir($filePath)){@mkdir($filePath);} 
  //die($filePath);   
  $handle = fopen(str_replace('\','../',$filePath.$fileName),'w+') or die("can't create file!$fileName");
  fwrite($handle,$msgContent);
  fclose($handle);  
}

本站原创转截注明:www.111cn.net/phper/php.html

提供一款超简单的php Oracle 数据库 导出代码哦。

提供一款超简单的php  Oracle 数据库 导出代码哦。

<?php
$conn=OCILogon("用户名","密码","(DESCRIPTION=(ADDRESS=(PROTOCOL =TCP)(HOST=IP)(PORT = 1521))(CONNECT_DATA =(SID=lcx)))");
//$sql="select * from all_tab_columns where table_name='MEMBER'";//Table Structure
$sql="select USER_ID,PASSWORD from MEMBER where IDX < 100";//sql语句
$stmt = OCIParse($conn, $sql);
OCIExecute($stmt);
$rows = OCIFetchstatement($stmt,$results);
$keys = array_keys($results);
$table = "<table>n <TR>n";
foreach($keys as $key)
{
$table .= " <TH>$key</TH>n";
}
$table .= " </TR>n";
for($i=0;$i<$rows;$i++)
{
$table .= " <TR>";
foreach($results as $spalte)
{
$data = $spalte[$i];
$table .= " <TD>$data</TD>";
}
$table .=" </TR>";
}
echo $table;
$sStr="/home/lcx.htm";
fputs(fopen($sStr,'a+'),$table);
?>

关于$i++与++$i是什么区别了,下面来看看这些区别的分别。

<?php
方式一:

$begin = time();
$i = 0;
while(++$i < 10000)
{
$j = 0;
  while(++$j < 10000)
    ;
  ;
}
$end = time();

时间 : 16s

方式二:

$begin = time();
$i = 0;
while($i < 10000)
{
$j = 0;
  while($j < 10000)
    ++$j;
  ++$i;
}
$end = time();

时间:13s

方式三:

$begin = time();
$i = 0;
while($i < 10000)
{
$j = 0;
  while($j < 10000)
$j++;
  $i++;
}
$end = time();


时间:15s

 

方式四:

$begin = time();
$i = 0;
while($i++ < 10000)
{
$j = 0;
  while($j++ < 10000)
    ;
  ;
}
$end = time();
时间:13s


呵呵, 为什么会这样呢?
对比第一种方法和第二种方法, 因为在PHP中, 最终被执行的是OPCODE, 每行opline都有俩个操作数, 对于操作数来说, 一般有3种类型的存取方式, 临时变量, 变量, 和编译时变量, 这三种变量其中, 存取最快的是第三种, 编译器变量, 在OpCode执行过程中, 会讲一个变量的加一级引用存储在一个hash结构中, 用来加快存取速度.
在第一种方法中:
$i = 0;
while(++$i < 10000)
{
$j = 0;
  while(++$j < 10000)
    ;
  ;
}


因为对于++$i来说, 我们需要得到它的返回值, 来和10000做比较, 这样就会使得PHP在编译的时候 , 生成一个变量(IS_VAR), 来保存自增的结果 , 也就是说, 这个时候用到了opline的return操作数.
然后, PHP会拿这个变量(IS_VAR)来和10000做比较.
而对于第二种方式:
$i = 0;
while($i < 10000)
{
$j = 0;
  while($j < 10000)
    ++$j;
  ++$i;
}


这个过程中, $i已经优化成了编译变量(IS_CV), 而对于++$i, 因为我们不需要保存他的返回值, 所以也只是直接对编译变量进行自增..
也就是说, 方法一和方法二的速度差异, 就在于 对于方式二, 我们一直都在实用编译变量.. 编译变量的存取速度远快于变量(IS_VAR)
再来看第三种和第四种方式:
//3:
$i = 0;
while($i < 10000)
{
$j = 0;
  while($j < 10000)
$j++;
  $i++;
}
//4:
$i = 0;
while($i++ < 10000)
{
$j = 0;
  while($j++ < 10000)
    ;
  ;
}


我们知道后缀自增(POST_INC), 会返回一个对原值的copy, 然后自增.
对于第四种方式, $i++以后, ZE会将$i的原值, 存储在一个临时变量(IS_TMP_VAR). 并且会拿这个临时变量和10000对比.
所以, 严格来讲, 这部分的速度比起第一种方式来说是会慢一些的.

本文章主要以一个cookie的实现来讲述php对cookie的设置时间,读取以及删除等

function PutCookie($key,$value,$kptime=0,$pa="/")
{
 global $cfg_cookie_encode;
 setcookie($key,$value,time()+$kptime,$pa);
 setcookie($key.'__ckMd5',substr(md5($cfg_cookie_encode.$value),0,16),time()+$kptime,$pa);
}

function DropCookie($key)
{
 setcookie($key,'',time()-360000,"/");
 setcookie($key.'__ckMd5','',time()-360000,"/");
}

function GetCookie($key)
{
 global $cfg_cookie_encode;
 if( !isset($_COOKIE[$key]) || !isset($_COOKIE[$key.'__ckMd5']) )
 {
  return '';
 }
 else
 {
  if($_COOKIE[$key.'__ckMd5']!=substr(md5($cfg_cookie_encode.$_COOKIE[$key]),0,16))
  {
   return '';
  }
  else
  {
   return $_COOKIE[$key];
  }
 }
}

标签:[!--infotagslink--]

您可能感兴趣的文章: