首页 > 编程技术 > php

php textarea换行符的方法

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

涉及到textarea中的换行,因为填入的内容是base64编码之后的结果(规范规定,base64编码结果中每76个字符需要加上一个回车换行,即一个" "),在ie下测试,一切ok,但是到了firefox和谷歌浏览器下,总是提示错误,细查一下,发现不同浏览器对于换行符处理不同造成的。

在ie下,换行符传入

在firefox和谷歌浏览器下,换行符只传入了

因为后台程序默认是按照处理的,所以字符串就不匹配了。
解决办法:如果发现传入的字符串只有,没有,则用正则表达式将替换成,然后再进行业务处理即可。

 

3.用php函数,str_replace替换函数,比如str_replace(‘ ','<br />') ,问题是替换并没有成功,一直都没有替换掉,我弄了好久甚至开始怀疑textarea中的换行符是不是,现在想想真是太动摇了,其实我经过测 分别替换 /n 后直觉告诉我自己走进了死胡同,这不是解决问题的方式,肯定有哪里存在根本性错误。突然我想到了会不会是单引号或者双引号的问题呢,于是将str_replace(‘ ','<br />') 改成str_replace(“ ”,”<br />”),灵光闪现了,替换成功


序列
含义


换行(lf 或 ascii 字符 0×0a(10))


回车(cr 或 ascii 字符 0×0d(13))


水平制表符(ht 或 ascii 字符 0×09(9))

\
反斜线

$
美元符号


双引号

[0-7]{1,3}
此正则表达式序列匹配一个用八进制符号表示的字符

x[0-9a-fa-f]{1,2}
此正则表达式序列匹配一个用十六进制符号表示的字符

三个函数吧:"ob_start()、ob_end_clean()、ob_get_contents()"

ob_start():是打开缓冲区的,就是要把您需要生成的静态文件的内容缓存在这里;
ob_get_contents():是读出缓冲区里的内容,下面有代码为例;
ob_end_clean():这个比较重要,只有使用了这个函数后,缓冲区里的内容才会读取出来;


以下为引用的内容:

 代码如下 复制代码

if(file_exists("./index.htm"))//看静态index.htm文件是否存在
{
$time=time();

//文件修改时间和现在时间相差?的话,直接导向htm文件,否则重新生成htm
if($time-filemtime("./index.htm")< 600)
{
header("location:classhtml/main.htm");

}
}

//在你的开始处加入ob_start();

 代码如下 复制代码
ob_start();

//首页内容,就是你的动态部分了

//在结尾加入ob_end_clean(),并把本页输出到一个变量中

 代码如下 复制代码
$temp=ob_get_contents();
ob_end_clean();

//写入文件

 代码如下 复制代码
$fp=fopen("./index.htm",w);
fwrite($fp,$temp) or die(写文件错误);


//echo"生成html完成!";

这二款文件上传程序应该是php 中文件上传功能最简单的吧,基本没做任何判断哦,直接把文件上传到了服务器哦。

$sort=12;
$f_type=strtolower("swf,jpg,rar,zip,7z,iso,gif");//设置可上传的文件类型
$file_size_max=200*1024*1024;//限制单个文件上传最大容量
$overwrite = 0;//是否允许覆盖相同文件,1:允许,0:不允许
$f_input="files";//设置上传域名称
    foreach($_files[$f_input]["error"] as $key => $error){
        $up_error="no";
        if ($error == upload_err_ok){
            $f_name=$_files[$f_input]['name'][$key];//获取上传源文件名
   
            $uploadfile=$uploaddir.strtolower(basename($f_name));
            
            $tmp_type=substr(strrchr($f_name,"."),1);//获取文件扩展名
   $tmp_type=strtolower($tmp_type);
            if(!stristr($f_type,$tmp_type)){
                echo "<script>alert('对不起,不能上传".$tmp_type."格式文件, ".$f_name." 文件上传失败!')</script>";
                $up_error="yes";
            }
            
            if ($_files[$f_input]['size'][$key]>$file_size_max) {
   
                echo "<script>alert('对不起,你上传的文件 ".$f_name." 容量为".round($_files[$f_input]
['size'][$key]/1024)."kb,大于规定的".($file_size_max/1024)."kb,上传失败!')</script>";
                $up_error="yes";
            }
            
            if (file_exists($uploadfile)&&!$overwrite){
                echo "<script>alert('对不起,文件 ".$f_name." 已经存在,上传失败!')</script>";
                $up_error="yes";
            }
             $string = 'abcdefghijklmnopgrstuvwxyz0123456789';
$rand = '';
for ($x=0;$x<12;$x++)
  $rand .= substr($string,mt_rand(0,strlen($string)-1),1);
$t=date("ymdhis").substr($gettime[0],2,6).$rand;
$attdir="./file/"; 
    if(!is_dir($attdir))  
    {  mkdir($attdir);}
            $uploadfile=$attdir.$t.".".$tmp_type;
            if(($up_error!="yes") and (move_uploaded_file($_files[$f_input]['tmp_name']

[$key], $uploadfile))){

                
    $_msg=$_msg.$f_name.'上传成功 ';
    
    
            }
   else{
   $_msg=$_msg.$f_name.'上传失败 ';
   }
        }
 
    }
echo "<script>window.parent.finish('".$_msg."');</script>"; 
?>

第二款文件上传代码

<?php
   $destination_path = '../../upfile/jianjulogo/';//getcwd().directory_separator;

   $result = 0;
  
   $target_path = $destination_path . basename( $_files['myfile']['name']);

   if(@move_uploaded_file($_files['myfile']['tmp_name'], $target_path)) {
      $result = 1;
   }
   echo $target_path;
 sleep(1);


?>
<script language="网页特效" type="text/javascript">window.top.window.stopupload(<?php echo $result; ?>,'<?=$target_path?>');</script>

这是一款自定义 的php文件新建函数,writer(文件名,写入数据, 写入数据方式);,用户可能根据自己的需要来定义创建文件的属性哦。

 * 写入函数
 * writer(文件名,写入数据, 写入数据方式);
 */

 代码如下 复制代码
 function writer(
    $filename,
    $data = '',
    $mode='w'
   ){
  if(trim($filename)){
   $file = @fopen($filename, $mode);
   @fwrite($file, $data);
   @fclose($file);
  }
  if(!is_file($filename)){
   die(et_e_not_write1.$filename.et_e_not_write2);
  }
 }

 

 代码如下 复制代码

$files ='<img" width=100% src="http://img.111cn.net/img/bid_v2/bid_v2_content/p_bid.gif" alt="普通任务" /><img width="272px" height="60px" style="padding-top: 10px;"" width=100% src="http://img.imgzhubajie.com/img/index_v3/20100611001.gif">';
// 图片地址转换一下   $p=preg_replace('//image//', 'http://qq.ip138.com/image/', $pg[1]);
 $reg = "/<img[^>]*src="(http://(.+)/(.+).(jpg|gif|bmp|bnp))"/isu";
$img=preg_match_all($reg,$files,$imgs);

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

//echo "".$imgs[1][$i]." <br> ";
//保存图片
$opts = array(
  'http'=>array(
    'header'=>"referer: http://www.111cn.net/ "
  )
);
}

标签:[!--infotagslink--]

您可能感兴趣的文章: