简体   繁体   English

上载.html文件时,从html中删除脚本标签

[英]Remove Script tag from html while uploading .html file

I have given a browse button to upload a file. 我给了一个浏览按钮来上传文件。 I want to remove script tag & it's content while uploding .html file & then upload html file. 我要删除脚本标记及其内容,同时将.html文件上载然后上传html文件。

If anybody have solution please suggest me. 如果有人有解决方案,请建议我。

Thanks 谢谢

You can just use .replace or preg_replace for Javascript and PHP respectively. 您可以分别将.replacepreg_replace分别用于Javascript和PHP。

Your regex pattern would be <script.*</script> 您的正则表达式模式为<script.*</script>

On the client side.. you can append the html to an iframe 在客户端..您可以将html附加到iframe

Then, try something like.. 然后,尝试类似..

$(..documentElement).find('script').remove();

var htmlContent = $(..documentElement).html();

Then pass the html over to server-side. 然后将html传递到服务器端。 Haven't tried it, just a suggestion. 还没有尝试过,只是一个建议。

  1. Upload the file 上载档案
  2. Read the text of the file with fread() 使用fread()读取文件的文本
  3. Edit the variable that contains the text of the file, delete from variable the <script> - use this: remove script tag from HTML content 编辑包含文件文本的变量,从变量<script> 删除 -使用此方法: 从HTML内容中删除脚本标签
  4. Write the new text to the file with fwrite() 使用fwrite()将新文本写入文件
             class html
{
    var $dir;
    var $rootdir;
    var $name;
    var $dirname;
    var $url;
    var $time;
    var $dirtype;
    var $nametype;

    function html($nametype='name',$dirtype='year',$rootdir='html')
    {
        $this->setvar($nametype,$dirtype,$rootdir);
    }

    function setvar($nametype='name',$dirtype='year',$rootdir='html')
    {
      $this->rootdir=$rootdir;
      $this->dirtype=$dirtype;
      $this->nametype=$nametype;
    }

    function createdir($dir='')
    {
        $this->dir=$dir?$dir:$this->dir;

        if (!is_dir($this->dir))
        {
            $temp = explode('/',$this->dir);
            $cur_dir = '';
            for($i=0;$i<count($temp);$i++)
            {
                $cur_dir .= $temp[$i].'/';
                if (!is_dir($cur_dir))
                {
                @mkdir($cur_dir,0777);
                }
            }
        }
    }

    function getdir($dirname='',$time=0)
    {
        $this->time=$time?$time:$this->time;
        $this->dirname=$dirname?$dirname:$this->dirname;

        switch($this->dirtype)
        {
        case 'name': 

        if(empty($this->dirname))
           $this->dir=$this->rootdir;fenzu.qqq80.com
        else
           $this->dir=$this->rootdir.'/'.$this->dirname;
        break;
        case 'year':
        $this->dir=$this->rootdir.'/'.date("Y",$this->time);
        break;

        case 'month':
        $this->dir=$this->rootdir.'/'.date("Y-m",$this->time);
        break;

        case 'day':
        $this->dir=$this->rootdir.'/'.date("Y-m-d",$this->time);
        break;
        }

        $this->createdir();

        return $this->dir;
    }

    function geturlname($url='')
    {
        $this->url=$url?$url:$this->url;

        $filename=basename($this->url);
        $filename=explode(".",$filename);
        return $filename[0];
    }

    function geturlquery($url='')
    {
        $this->url=$url?$url:$this->url;

        $durl=parse_url($this->url);
        $durl=explode("&",$durl[query]);
        foreach($durl as $surl)
        {
          $gurl=explode("=",$surl);
          $eurl[]=$gurl[1];
        }
        return join("_",$eurl);
    }

    function getname($url='',$time=0,$dirname='')
    {
        $this->url=$url?$url:$this->url;
        $this->dirname=$dirname?$dirname:$this->dirname; 

        $this->time=$time?$time:$this->time;

        $this->getdir();

        switch($this->nametype)
        {
        case 'name':
        $filename=$this->geturlname().'.htm';
        $this->name=$this->dir.'/'.$filename;
        break;

        case 'time':
        $this->name=$this->dir.'/'.$this->time.'.htm';
        break;

        case 'query':
        $this->name=$this->dir.'/'.$this->geturlquery().'.htm';
        break;

        case 'namequery':
        $this->name=$this->dir.'/'.$this->geturlname().'-'.$this->geturlquery().'.htm';
        break;

        case 'nametime':
        $this->name=$this->dir.'/'.$this->geturlname().'-'.$this->time.'.htm';
        break;

        }
        return $this->name;
    }

    function createhtml($url='',$time=0,$dirname='',$htmlname='')
    {
        $this->url=$url?$url:$this->url;
        $this->dirname=$dirname?$dirname:$this->dirname;
        $this->time=$time?$time:$this->time;

        if(empty($htmlname))
            $this->getname();
        else
            $this->name=$dirname.'/'.$htmlname;

        $content=file($this->url) or die("Failed to open the url ".$this->url." !");;
        $content=join("",$content);
        $fp=@fopen($this->name,"w") or die("Failed to open the file ".$this->name." !"); 

        if(@fwrite($fp,$content))
        return true;
        else
        return false;
        fclose($fp);
    }

    function deletehtml($url='',$time=0,$dirname='')
    {
        $this->url=$url?$url:$this->url;
        $this->time=$time?$time:$this->time;

        $this->getname();

        if(@unlink($this->name))
        return true;
        else
        return false;
    }

    /**
     * function::deletedir()
     * 删除目录
     * @param $file 目录名(不带/)
     * @return
     */
     function deletedir($file)
     {
        if(file_exists($file))
        {
            if(is_dir($file))
            {
                $handle =opendir($file);
                while(false!==($filename=readdir($handle)))
                {
                    if($filename!="."&&$filename!="..")
                      $this->deletedir($file."/".$filename);
                }
                closedir($handle);
                rmdir($file);
                return true;
            }
            else
            { 

                unlink($file);
            }
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM