OpenTbs将html标记转换为MS Word标记 [英] OpenTbs convert html tags to MS Word tags

查看:116
本文介绍了OpenTbs将html标记转换为MS Word标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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\">";
    }
}

这篇关于OpenTbs将html标记转换为MS Word标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆