Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用XML将html标记转换为docx并更新TOC_Php_Xml_Ms Word - Fatal编程技术网

在php中使用XML将html标记转换为docx并更新TOC

在php中使用XML将html标记转换为docx并更新TOC,php,xml,ms-word,Php,Xml,Ms Word,我需要实现一个在PHP中将html导出到docx文档的模块。我创建了一个模板,并在其中设置了一些变量。我将这些变量替换为从数据库查询的数据。当需要添加一些带有样式属性和TOC的html标记时,它正在工作。我使用str_replace转换一些简单的标记,如、等,但是如果添加样式属性(如align和color),则无法工作 是否有现成的开源系统可以将html标记(包括其样式)转换为word 我可以在完成所有替换后创建TOC吗 我过去经常这样做 我使用它的方式是:上传一个现有的文档,其中有%name%

我需要实现一个在PHP中将html导出到docx文档的模块。我创建了一个模板,并在其中设置了一些变量。我将这些变量替换为从数据库查询的数据。当需要添加一些带有样式属性和TOC的html标记时,它正在工作。我使用str_replace转换一些简单的标记,如

等,但是如果添加样式属性(如align和color),则无法工作

  • 是否有现成的开源系统可以将html标记(包括其样式)转换为word

  • 我可以在完成所有替换后创建TOC吗

  • 我过去经常这样做

    我使用它的方式是:上传一个现有的文档,其中有
    %name%
    这样的标签。 这些标记将替换为
    $name
    变量,系统将输出新文档

    为了修复phpword无法替换变量的错误,我不得不修改
    Template.php
    文件。查找方法
    setValue
    ,并将函数更改为:

    $pattern = '|\$\{([^\}]+)\}|U';
    preg_match_all($pattern, $this->_documentXML, $matches);
    $openedTagPattern= '/<[^>]+>/';
    $closedTagPattern= '/<\/[^>]+>/';
    foreach ($matches[0] as $value) {
        $modified= preg_replace($openedTagPattern, '', $value);
        $modified= preg_replace($closedTagPattern, '', $modified);
    
        $this->_header1XML = str_replace($value, $modified, $this->_header1XML);
        $this->_header2XML = str_replace($value, $modified, $this->_header2XML);
        $this->_header3XML = str_replace($value, $modified, $this->_header3XML);
        $this->_documentXML = str_replace($value, $modified, $this->_documentXML);
        $this->_footer1XML = str_replace($value, $modified, $this->_footer1XML);
        $this->_footer2XML = str_replace($value, $modified, $this->_footer2XML);
        $this->_footer3XML = str_replace($value, $modified, $this->_footer3XML);
    }
    
    if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
        $search = '${'.$search.'}';
    }
    
    if(!is_array($replace)) {
        $replace = utf8_encode($replace);
    }
    
    $this->_header1XML = str_replace($search, $replace, $this->_header1XML);
    $this->_header2XML = str_replace($search, $replace, $this->_header2XML);
    $this->_header3XML = str_replace($search, $replace, $this->_header3XML);
    $this->_documentXML = str_replace($search, $replace, $this->_documentXML);
    $this->_footer1XML = str_replace($search, $replace, $this->_footer1XML);
    $this->_footer2XML = str_replace($search, $replace, $this->_footer2XML);
    $this->_footer3XML = str_replace($search, $replace, $this->_footer3XML);
    
    $pattern='\$\{([^\}]+)\}U';
    preg_match_all($pattern,$this->\u documentXML,$matches);
    $openedTagPattern='/]+>/';
    $closedTagPattern='/]+>/';
    foreach($将[0]匹配为$value){
    $modified=preg_replace($openedTagPattern',,$value);
    $modified=preg_replace($closedTagPattern,,$modified);
    $this->\u header1XML=str\u replace($value,$modified,$this->\u header1XML);
    $this->_header2XML=str_replace($value,$modified,$this->_header2XML);
    $this->\u header3XML=str\u replace($value,$modified,$this->\u header3XML);
    $this->\u documentXML=str\u replace($value,$modified,$this->\u documentXML);
    $this->\u footer1XML=str\u replace($value,$modified,$this->\u footer1XML);
    $this->\u footer2XML=str\u replace($value,$modified,$this->\u footer2XML);
    $this->\u footer3XML=str\u replace($value,$modified,$this->\u footer3XML);
    }
    if(substr($search,0,2)!='${'&&substr($search,-1)!='}')){
    $search='${.$search.'}';
    }
    如果(!是_数组($replace)){
    $replace=utf8\u编码($replace);
    }
    $this->\u header1XML=str\u replace($search,$replace,$this->\u header1XML);
    $this->_header2XML=str_replace($search,$replace,$this->_header2XML);
    $this->\u header3XML=str\u replace($search,$replace,$this->\u header3XML);
    $this->\u documentXML=str\u replace($search,$replace,$this->\u documentXML);
    $this->\u footer1XML=str\u replace($search,$replace,$this->\u footer1XML);
    $this->\u footer2XML=str\u replace($search,$replace,$this->\u footer2XML);
    $this->\u footer3XML=str\u replace($search,$replace,$this->\u footer3XML);
    
    它不工作。我在Word中添加了变量${name},并使用phpword函数setvalue替换了它,但是如果我使用html标记替换,它会生成错误的文档。引发一个关于_documentXML的未定义属性的异常。我已经添加了这些属性,并没有错误消息,但文件并没有保存。您是否遗漏了一些内容?这对我很有用。我们在这样的生产环境中使用它。请确保阅读文档以正确使用脚本。