简体   繁体   English

PHP DOM文档忽略附加nodeValue的空白

[英]PHP DOM Document ignore whitespaces appending nodeValue

I'm working with MS Office Word document through PHP and DOM. 我正在通过PHP和DOM处理MS Office Word文档。 I am adding paragraphs to my document. 我要在文档中添加段落。 And now I have to make the part of string bold (it becomes from database and I'm unable to change it). 现在,我必须将字符串的部分设为粗体(它已从数据库中删除,无法更改)。 Like this: 像这样:

The part of string is bold really. 字符串的部分确实是粗体的

What I do: 我所做的:

    if(strpos($input, $search)) {
            $splitted  = explode($search, $input);
            $t1 = new DOMElement('t', " ".$splitted[0]." ", $this->ns);
            $t2 = new DOMElement('t', " ". $search ." ", $this->ns);
            $t3 = new DOMElement('t', " ".$splitted[1]." ", $this->ns);
}

And after I'm adding this element to document. 然后,我将此元素添加到文档中。 BUut I'm getting: 但是我得到了:

The part of string is bold really. 字符串的部分确实是粗体的

It removes whitespaces before and after nodeValue. 它删除nodeValue之前和之后的空格。 I tried to force adding of spaces (as in code above). 我试图强制添加空格(如上面的代码所示)。 Nothing helps. 没有任何帮助。 What can I do? 我能做什么?

You can replace actual space characters with an   您可以用 替换实际的空格字符  Which doesn't break the line, but it adds a space. 不会打破界限,但会增加一个空格。

if(strpos($input, $search)) {
        $splitted  = explode($search, $input);
        $t1 = new DOMElement('t', ' '.$splitted[0].' ', $this->ns);
        $t2 = new DOMElement('t', ' '. $search .' ', $this->ns);
        $t3 = new DOMElement('t', ' '.$splitted[1].' ', $this->ns);
}

Answers on this question solved the broblem 这个问题的答案解决了问题

Te solution is: 解决方案是:

$t1 = new DOMElement('t', $splitted[0], $this->ns);
$t2 = new DOMElement('t', "\xC2\xA0". $search ."\xC2\xA0", $this->ns);
$t3 = new DOMElement('t', $splitted[1], $this->ns);

Because   因为  is N HTML entity. 是N个HTML实体。 For adding UTF-8 encoded non_breakable_space we have to use "\\xC2\\xA0" 要添加UTF-8编码的non_breakable_space,我们必须使用"\\xC2\\xA0"

Try replacing your spaces in strings with an " " string. 尝试用“”字符串替换字符串中的空格。 This is HTML equivalent for a "space" so to speak. 可以说,这等效于“空格”的HTML。

so instead of: 所以代替:

$t1 = new DOMElement('t', " ".$splitted[0]." ", $this->ns);

do: 做:

$t1 = new DOMElement('t', " ".$splitted[0]." ", $this->ns);

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

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