Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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
PHPWord如何替换HTML标记_Php_Html_Phpword - Fatal编程技术网

PHPWord如何替换HTML标记

PHPWord如何替换HTML标记,php,html,phpword,Php,Html,Phpword,我正在使用PHPWord创建word文档。文档的内容是动态的,内容可能包含如下HTML标记: <strong>Problem statement</strong> <p>The text may be<em>bold</em> subject to very peculiar conditions</p> <ul> <li>Test 1</li> <li>T

我正在使用PHPWord创建word文档。文档的内容是动态的,内容可能包含如下HTML标记:

<strong>Problem statement</strong>

<p>The text may be<em>bold</em> subject to very peculiar conditions</p>

<ul>
    <li>Test 1</li>
    <li>Test 2</li>
</ul>
问题陈述
根据非常特殊的条件,文本可以粗体显示

  • 测试1
  • 测试2
文档正在创建,但文档中的内容显示html标记


如何用适当的word文档标记替换这些标记,以便word文档以适当的格式显示与HTML视图完全相同的内容

我建议做一个这样的函数:

function convertTags($text){
        $text= str_replace("<strong>", "<w:b val="true"/>", $text);
        $text= str_replace("</string>", "<w:b val="false"/>", $text);
        // and all the tags like that. You may use an array and loop through it.
       return $text;
    }
函数转换标签($text){
$text=str_replace(“”,“”,$text);
$text=str_replace(“,”,$text);
//所有的标签都是这样的。你可以使用一个数组并循环通过它。
返回$text;
}

使用
str\u replace
将html标记替换为单词标记。例如:

$str = "<b>Hello World</b>";
$str = str_replace("<b>", "<strong>", $str);
$str = str_replace("</b>", "</strong>", $str);
//Gives "<strong>Hello World</strong>"
$str=“你好世界”;
$str=str_替换(“,”“,$str);
$str=str\u替换(“,”“,$str);
//给出“你好,世界

阅读更多内容

你的答案很好地说明了为什么使用
str_replace
和正则表达式在使用/解析或以其他方式操纵标记时是个坏主意:输入错误会让你失望,格式错误的标记也会让你失望,更不用说空格和标记了。只是解析HTMLI尝试了这个,但在相应的文档文件中,它以如下方式显示:有问题的背景statement@MohammadAbdullahJunayeed当前位置你看过我答案底部的便条了吗?看起来努拉齐耶使用的标签和我一样(哪些是open office使用的,不确定MS office使用什么,因此它们可能不起作用。我还再次强烈建议您寻找让word按原样呈现HTML的方法,就像复制粘贴网站时一样,这可能是最简单的解决方法。仍然有太多事情需要简单的
stru替换
无法处理:大写标记(例如
很简单,但
..)、属性、空格(
)。很快,您将为每个标记
str_replace([“,,…],“,$html”)生成一个huuuge数组
,您必须为每个标记调用
str\u replace
,您应该知道自己做错了什么。当然,您可能会考虑使用正则表达式来解决大小写和空白问题,但这样做。不要。做。它。TM处理标记的唯一正确方法是使用解析器