首页 > 编程技术 > php

php 中英文字符串截取函数

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

 1 /**
 2  * php教程获取字符串中英文混合长度
 3  * @param $str string 字符串
 4  * @param $$charset string 编码
 5  * @return 返回长度,1中文=1位,2英文=1位
 6  */
 7 function strlength($str,$charset='utf-8'){
 8     if($charset=='utf-8') $str = iconv('utf-8','gb2312',$str);
 9     $num = strlen($str);
10     $cnnum = 0;
11     for($i=0;$i<$num;$i++){
12         if(ord(substr($str,$i+1,1))>127){
13             $cnnum++;
14             $i++;
15        }
16     }
17     $ennum = $num-($cnnum*2);
18     $number = ($ennum/2)+$cnnum;
19     return ceil($number);
20 }
21
22 //测试输出长度都为15
23 $str1 = '测试测试测试测试测试测试测试测';
24 $str2 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
25 $str3 = 'aa测试aa测试aa测试aa测试aaaaaa';
26 echo strlength($str1,'gb2312');
27 echo strlength($str2,'gb2312');
28 echo strlength($str3,'gb2312');

php教程简单文件上传$_files与move_uploaded_file实现文件上传

$_files["file"]["name"] - 被上传文件的名称
$_files["file"]["type"] - 被上传文件的类型
$_files["file"]["size"] - 被上传文件的大小,以字节计
$_files["file"]["tmp_name"] - 存储在服务器的文件的临时副本的名称
$_files["file"]["error"] - 由文件上传导致的错误代码
这是一种非常简单文件上传方式。基于安全方面的考虑,您应当增加有关什么用户有权上传文件的限制。

注释:对于 ie,识别 jpg 文件的类型必须是 pjpeg,对于 firefox,必须是 jpeg。

 

/*  
*$formname                       html上传表单名称  
*$allowtype_array                允许上传的文件类型数组  
*$allow_size                     允许上传的文件最大大小,默认是1mb  
*$isrename                       是否需要重命名被上传的文件  
*/  
function upload_file($formname,$path,$allowtype_array,$allow_size='1048576',$isrename=true){  
 if($_files[$formname]['tmp_name']=='' || $_files[$formname]['name']=='' || $_files[$formname]['size']==0){  
  return false;  
 }  
 if(!file_exists('../uploadfiles/'.$path)){  
  if(!mkdir('../uploadfiles/'.$path,0777)){  
   writeerrmsg('创建上传文件保存文件目录失败,请联系管理员检查目录权限');  
  }  
 }  
 if($_files[$formname]['size']>$allow_size){  
  writeerrmsg('上传文件过大,请将上传文件限制在'.number_format($allow_size/1048576,'2','.','').'mb以内');  
 }  
 $file_name_array=explode('.',$_files[$formname]['name']);  
 $file_type=strtolower($file_name_array[count($file_name_array)-1]);  
 if(!in_array($file_type,$allowtype_array)){  
  writeerrmsg('上传文件类型错误,仅允许上传后缀名为 .'.implode(' .',$allowtype_array).'的文件');  
 }  
 $save_name=$isrename?randomname().'.'.$file_type:$_files[$formname]['name'];  
 if(!move_uploaded_file($_files[$formname]['tmp_name'],'../uploadfiles/'.$path.'/'.$save_name)){  
  writeerrmsg('文件上传过程中发生错误,请重新上传');  
 }  
 switch($_files[$formname]['error']){  
  case 0:  
   return $save_name;  
  break;  
  case 1:  
   writeerrmsg('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');  
  break;  
  case 2:  
   writeerrmsg('上传文件的大小超过了 html 表单中 max_file_size 选项指定的值');  
  break;  
  case 3:  
   writeerrmsg('文件只有部分被上传');  
  break;  
  case 4:  
   writeerrmsg('没有文件被上传');  
  break;  
  default:  
   writeerrmsg('能出现这条错误提示,日子也该混到头了');  
  break;  
 }  
}

move_uploaded_file
本函数检查并确保由 file 指定的文件是合法的上传文件(即通过 php 的 http post 上传机制所上传的)。如果文件合法,则将其移动为由 newloc 指定的文件。

如果 file 不是合法的上传文件,不会出现任何操作,move_uploaded_file() 将返回 false。

如果 file 是合法的上传文件,但出于某些原因无法移动,不会出现任何操作,move_uploaded_file() 将返回 false,此外还会发出一条警告。

这种检查显得格外重要,如果上传的文件有可能会造成对用户或本系统的其他用户显示其内容的话

<?php教程
/**
 * @author xiaoxiao <x_824@sina.com> 2011-1-12
 * @link http://xiaoyaoxia.cnblogs.com/
 * @license
 * 统计目录下的文件行数及总文件数··去除注释
 */

$obj = new caculatefiles();
//如果设置为false,这不会显示每个文件的信息,否则显示
$obj->setshowflag(false);
//会跳过所有all开头的文件
$obj->setfileskip(array('all'));
$obj->run("d:phpappphp_tests");

//所有文件,(默认格式为.php)
$obj->setfileskip(array());
$obj->run("d:phpappphp");

$obj->setshowflag(true);
//跳过所有i和a开头的文件,(比如接口和抽象类开头)
$obj->setfileskip(array('i', 'a'));
$obj->run("d:phpappphp");


/**
 * 执行目录中文件的统计(包括文件数及总行数
 *
 * 1、跳过文件的时候:
 *    匹配的规则只是从文件名上着手,匹配的规则也仅限在开头。
 * 2、跳过文件中的注释行:
 *    匹配的规则只是从注释段落的头部匹配,如果出现// 及 *及 #及/*开头的行及空行会被跳过。所以类似/*这种多汗注释,每行的开头都必须加上*号,否则无法匹配到这种的注释。
 * 3、目录过滤:
 *    匹配的规则是从目录名的全名匹配
 */
class caculatefiles {
 /**
  * 统计的后缀
  */
 private $ext = ".php";
 /**
  * 是否显示每个文件的统计数
  */
 private $showeveryfile = true;
 /**
  * 文件的的跳过规则
  */
 private $fileskip = array();
 /**
  * 统计的跳过行规则
  */
 private $lineskip = array("*", "/*", "//", "#");
 /**
  * 统计跳过的目录规则
  */
 private $dirskip = array(".", "..", '.svn');
 
 public function __construct($ext = '', $dir = '', $showeveryfile = true, $dirskip = array(), $lineskip = array(), $fileskip = array()) {
  $this->setext($ext);
  $this->setdirskip($dirskip);
  $this->setfileskip($fileskip);
  $this->setlineskip($lineskip);
  $this->setshowflag($showeveryfile);
  $this->run($dir);
 }
 
 public function setext($ext) {
  trim($ext) && $this->ext = strtolower(trim($ext));
 }
 public function setshowflag($flag = true) {
  $this->showeveryfile = $flag;
 }
 public function setdirskip($dirskip) {
  $dirskip && is_array($dirskip) && $this->dirskip = $dirskip;
 }
 public function setfileskip($fileskip) {
  $this->fileskip = $fileskip;
 }
 public function setlineskip($lineskip) {
  $lineskip && is_array($lineskip) && $this->lineskip = array_merge($this->lineskip, $lineskip);
 }
 /**
  * 执行统计
  * @param string $dir 统计的目录
  */
 public function run($dir = '') {
  if ($dir == '') return;
  if (!is_dir($dir)) exit('path error!');
  $this->dump($dir, $this->readdir($dir));
 }
   
 /**
  * 显示统计结果
  * @param string $dir 目录
  * @param array $result 统计结果(包含总行数,有效函数,总文件数
  */
 private function dump($dir, $result) {
  $totalline = $result['totalline'];
  $linenum = $result['linenum'];
  $filenum = $result['filenum'];
  echo "*************************************************************rn<br/>";
  echo $dir . ":rn<br/>";
  echo "totalline: " . $totalline . "rn<br/>";
  echo "totalline with no comment and empty: " . $linenum . "rn<br/>";
  echo 'totalfiles:' . $filenum . "rn<br/>";
 }

 /**
  * 读取目录
  * @param string $dir 目录
  */
 private function readdir($dir) {
  $num = array('totalline' => 0, 'linenum' => 0, 'filenum' => 0);
  if ($dh = opendir($dir)) {
   while (($file = readdir($dh)) !== false) {
    if ($this->skipdir($file)) continue;
    if (is_dir($dir . '/' . $file)) {
     $result = $this->readdir($dir . '/' . $file);
     $num['totalline'] += $result['totalline'];
     $num['linenum'] += $result['linenum'];
     $num['filenum'] += $result['filenum'];
    } else {
     if ($this->skipfile($file)) continue;
     list($num1, $num2) = $this->readfiles($dir . '/' . $file);
     $num['totalline'] += $num1;
     $num['linenum'] += $num2;
     $num['filenum']++;
    }
   }
   closedir($dh);
  } else {
   echo 'open dir <' . $dir . '> error!' . "r";
  }
  return $num;
 }

 /**
  * 读取文件
  * @param string $file 文件
  */
 private function readfiles($file) {
  $str = file($file);
  $linenum = 0;
  foreach ($str as $value) {
   if ($this->skipline(trim($value))) continue;
   $linenum++;
  }
  $totalnum = count(file($file));
  if (!$this->showeveryfile) return array($totalnum, $linenum);
  echo $file . "rn";
  echo 'totalline in the file:' . $totalnum . "rn";
  echo 'totalline with no comment and empty in the file:' . $linenum . "rn";
  return array($totalnum, $linenum);
 }
  
    /**
  * 执行跳过的目录规则
  * @param string $dir 目录名
  */
 private function skipdir($dir) {
  if (in_array($dir, $this->dirskip)) return true;
  return false;
 }
   
 /**
  * 执行跳过的文件规则
  * @param string $file 文件名
  */
 private function skipfile($file) {
  if (strtolower(strrchr($file, '.')) != $this->ext) return true;
  if (!$this->fileskip) return false;
  foreach ($this->fileskip as $skip) {
   if (strpos($file, $skip) === 0) return true;
  }
  return false;
 }
   
 /**
  * 执行文件中行的跳过规则
  * @param string $string 行内容
  */
 private function skipline($string) {
  if ($string == '') return true;
  foreach ($this->lineskip as $tag) {
   if (strpos($string, $tag) === 0) return true;
  }
  return false;
 }
}

php教程 数组元素随机排序代码
因为工作需要我要对我定义好的数组进行随机排序,每一次循环都要是不同的,下面我们利用了php shuffle函数随机对数组元素进行排序。方法非常简单。

shuffle() 函数把数组中的元素按随机顺序重新排列。

若成功,则返回 true,否则返回 false。

注释:本函数为数组中的单元赋予新的键名。这将删除原有的键名而不仅是重新排序。

*/
$array = array(1,2,3,4);
for( $i=1;$i<30;$i++)
{
 shuffle( $array );
 print_r($array);
 echo '<br />';
}
/*
array ( [0] => 3 [1] => 2 [2] => 4 [3] => 1 )
array ( [0] => 3 [1] => 4 [2] => 1 [3] => 2 )
array ( [0] => 2 [1] => 1 [2] => 3 [3] => 4 )
array ( [0] => 2 [1] => 4 [2] => 1 [3] => 3 )
array ( [0] => 3 [1] => 4 [2] => 2 [3] => 1 )
array ( [0] => 3 [1] => 2 [2] => 4 [3] => 1 )
array ( [0] => 3 [1] => 4 [2] => 1 [3] => 2 )
array ( [0] => 4 [1] => 2 [2] => 1 [3] => 3 )
array ( [0] => 2 [1] => 4 [2] => 1 [3] => 3 )
array ( [0] => 4 [1] => 3 [2] => 1 [3] => 2 )
array ( [0] => 3 [1] => 2 [2] => 1 [3] => 4 )
array ( [0] => 4 [1] => 3 [2] => 1 [3] => 2 )
array ( [0] => 4 [1] => 1 [2] => 2 [3] => 3 )
array ( [0] => 2 [1] => 1 [2] => 4 [3] => 3 )
array ( [0] => 3 [1] => 1 [2] => 4 [3] => 2 )
array ( [0] => 2 [1] => 1 [2] => 3 [3] => 4 )
array ( [0] => 4 [1] => 1 [2] => 3 [3] => 2 )
array ( [0] => 1 [1] => 4 [2] => 2 [3] => 3 )
array ( [0] => 3 [1] => 2 [2] => 1 [3] => 4 )
array ( [0] => 1 [1] => 2 [2] => 4 [3] => 3 )
array ( [0] => 1 [1] => 3 [2] => 4 [3] => 2 )
array ( [0] => 2 [1] => 4 [2] => 3 [3] => 1 )
array ( [0] => 3 [1] => 2 [2] => 1 [3] => 4 )
array ( [0] => 1 [1] => 3 [2] => 4 [3] => 2 )
array ( [0] => 2 [1] => 1 [2] => 4 [3] => 3 )
array ( [0] => 4 [1] => 1 [2] => 3 [3] => 2 )
array ( [0] => 3 [1] => 2 [2] => 1 [3] => 4 )
array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
array ( [0] => 2 [1] => 1 [2] => 4 [3] => 3 )

总结:
php有大量的函数,如果你知道这个函数会对你开发提高大量的帮助,如果你不知道要自己写可能花很多时间。
*/

php教程目录删除创建遍历

实例目录结构:php执行文件与leaps教程oulcn目录处在同一级,创建的子目录处在leapsoulcn目录下。
第一步:使用php目录函数创建相关目录
复制代码 代码如下:
<?
mkdir("leapsoulcn",0777);
mkdir("leapsoulcn/leapsoul",0777);
mkdir("leapsoulcn/php",0777);
mkdir("leapsoulcn/php/web",0777);
mkdir("leapsoulcn/php/web/test",0777);
?>

说明:在这段代码中,先使用php目录函数mkdir创建主目录leapsoulcn,并创建了两个子目录,leapsoul及php,在php目录下创建了web以及test目录。
知识点:mkdir主要用来创建目录,有两个参数:新目录名(注意创建多级目录时,必须包含目录路径),新目录的访问权限,即umask值,第一个数字通常是0,第二个数字指定了所有者特许,第三个数字指定了所有者用户群的特许 ,第四个数字制定了全局特许,可用值如下:
1 = 可执行
2 = 可写
4 = 可读
将三个数字加起来,7代表拥有所有权限,你可以根据自己的需要对创建的新目录赋予不同的权限。
第二步:在leapsoulcn/php/目录下创建leapsoulcn.txt文件,并写入相关的内容
复制代码 代码如下:
<?
@$fp = fopen("leapsoulcn/php/leapsoulcn.txt","w");
if(!$fp){
echo "system error";
exit();
}else {
$filedata = "domain"."t"."www.111cn.net"."n";
$filedata = $filedata."description"."t"."php网站开发教程网,面向php初学者的php教程网。"."n";
$filedata = $filedata."title"."t"."本实例主要讲述php目录函数的具体应用:涵盖读取目录、创建目录、删除目录等功能";
fwrite($fp,$filedata);
fclose($fp);
}
?>

说明:这段实例代码具体解释可参考之前介绍的php文件写入教程。
第三步:读取(遍历)目录名及文本文件名
复制代码 代码如下:
<?
$dir = opendir("leapsoulcn");
while ($filedir = readdir($dir)) {
if (!strcmp($filedir,".")||!strcmp($filedir,"..")) {
continue;
}
echo $filedir."目录列表:<br/><br/>";
$subdir = "leapsoulcn/".$filedir;
$dirc = "->";
listsubdir($subdir,$dirc);
}
closedir($dir);
?>

说明:在这段代码实例教程中主要使用了php目录函数opendir(),readdir(),closedir()。
知识点:
1、opendir函数用来打开所游览的具体目录,函数参数为目录名,注意,由于在本实例教程中php执行文件和游览的主目录处在同一级,所以传递的参数仅仅只是目录名,如果不在同一级或读取多级目录时,需带上具体的目录路径或文件路径。
2、在通过opendir函数读取了主目录后,通过while循环来进一步读取主目录下的多级目录及文件,此处使用的php目录函数为readdir,此函数从目录中读取目录或文件名,当没有可读取的目录或文件时,返回false,注意,读取的目录包含.和..,在本实例教程中由于是一级级往下读取目录,所以当读取的目录信息为.和..时跳出本次循环,继续读取下一级目录。
3、在读取完主目录的所有子目录及文件后,通过php目录函数closedir来关闭目录句柄,类似于fclose函数关闭文件。
第四步:创建读取(遍历)目录及文件的递归函数
复制代码 代码如下:
<?
function listsubdir($dirinfo,$dirc)
{
if (is_dir($dirinfo)) {
$subdir = dir($dirinfo);
while ($subfile = $subdir->read()) {
if (!strcmp($subfile,".")||!strcmp($subfile,"..")) {
continue;
}
$newdir = $dirinfo."/".$subfile;
if (is_file($newdir)) {
echo $dirc.$subfile.":文件属性<br/>";
}
else{
echo $dirc.$subfile.":目录属性<br/>";
listsubdir($newdir,"-".$dirc);
}
}
$subdir->close();
return;
}
else return;
}
?>

说明:此函数有两个参数:需要读取的目录(包含目录路径),显示用的多级目录分隔符。在这个函数中主要使用了php文件目录函数is_dir,is_file,dir类。
知识点:
1、首先通过is_dir来判断要读取的是目录还是文件,此函数的参数和opendir函数类似,注意目录路径问题。
2、如果判断需要读取的是目录,则通过dir目录类来进一步读取其多级子目录,层层递归。dir类所具有的操作函数功能和opendir、readdir、closedir这些php目录函数功能一致。
  至此整个创建目录,读取目录的代码实例就算完成了,可列出主目录下的多级子目录名及文本文件名。
如何删除目录?
  删除目录可以使用php目录函数rmdir,函数的参数和mkdir函数参数类似,可以使用相对目录路径或绝对目录路径,只是要删除的目录必须为空目录,通过上述代码实例你完全可以判断哪些是空目录。

标签:[!--infotagslink--]

您可能感兴趣的文章: