OpenTbs 将 html 标签转换为 MS Word 标签

2021-12-31 00:00:00 ms-word templates php tinybutstrong opentbs

我正在使用 OpenTbs,http://www.tinybutstrong.com/plugins/opentbs/tbs_plugin_opentbs.html.

I am using OpenTbs, http://www.tinybutstrong.com/plugins/opentbs/tbs_plugin_opentbs.html.

我有一个 template.docx 并且能够用内容替换字段,但如果内容有 html 代码,它会显示在模板创建的文档中.

I have a template.docx and am able to replace fields with content but if the content has html code it is displayed in the document created by the template.

First list <br /> Second Line

我曾尝试使用:

$TBS->LoadTemplate('document.docx', OPENTBS_ALREADY_XML); 

认为这将允许我用 ms office 标签替换我的 html 标签,但它只是在文档中显示了 MS Office 标签:

Thinking this would allow me to replace my html tags with ms office tags, but it just showed the MS Office tags in the document instead:

First Line<w:br/> Second Line

如何将 HTML 标记转换为等效的 MS Office XML.

How do i convert the HTML tags into the MS Office XML equivalent.

推荐答案

感谢 Skrol 对我所有 openTBS 问题的投入,只是注意到你是它的创建者,它是一个很棒的类,并且你上面说的在之后是正确的通过学习 MS Word 格式的一天我有一个脑波,我现在能够生成您在上面指定的格式,并且可以使用粗斜体和下划线,这是我所需要的,我希望这为您提供了改进的基础

Thanks Skrol for your input on all my openTBS issues, just noticed that you are the creator of it, its a great class and what you said above was true after a day of plowing through learning the MS Word Format i had a brain wave and I am now able to produce the format that you specified above and can have bold italic and underline which is all i require, I hope this gives you a foundation to improve upon.

我基本上注意到,在您放置的示例中,您只需要一个样式数组,当您找到一个结束标记时,您就可以从样式数组中删除它.每次您找到一个标签时,您都需要关闭 <w:r> 并创建一个新标签,我已经对其进行了测试,并且效果很好.

I basically noticed that in the example you put you just need an array of the styles which when you find a closing tag you remove from the style array. Each time you find a tag you need to close the <w:r> and create a new one, I have tested it and it works wonderfully.

class printClass {
    private static $currentStyles = array();    

    public function __construct() {}

    public function format($string) {
            if($string !=""){
            return preg_replace_callback("#<b>|<u>|<i>|</b>|</u>|</i>#",
                                        'printClass::replaceTags',
                                        $string);
        }else{
            return false;
        }
    }


    private static function applyStyles() {

        if(count(self::$currentStyles) > 0 ) {

            foreach(self::$currentStyles as $value) {

                if($value == "b") {
                    $styles .= "<w:b/>";
                }   

                if($value == "u") {
                    $styles .= "<w:u w:val="single"/>";
                }   

                if($value == "i") {
                    $styles .= "<w:i/>";
                }
            }

            return "<w:rPr>" . $styles . "</w:rPr>";
        }else{
            return false;
        }
    }



    private static function replaceTags($matches) {

        if($matches[0] == "<b>") {
            array_push(self::$currentStyles, "b");
        }   

        if($matches[0] == "<u>") {
            array_push(self::$currentStyles, "u");
        }   

        if($matches[0] == "<i>") {
            array_push(self::$currentStyles, "i");
        }

        if($matches[0] == "</b>") {
            self::$currentStyles = array_diff(self::$currentStyles, array("b"));
        }   

        if($matches[0] == "</u>") {
            self::$currentStyles = array_diff(self::$currentStyles, array("u"));
        }   

        if($matches[0] == "</i>") {
            self::$currentStyles = array_diff(self::$currentStyles, array("i"));
        }

        return "</w:t></w:r><w:r>" . self::applyStyles() . "<w:t xml:space="preserve">";
    }
}

相关文章