在PHP中的多跨XML中维护换行或不换行

  • 本文关键字:换行 维护 XML PHP php xml
  • 更新时间 :
  • 英文 :


我有一个类似的字符串

<p begin="00:35:47.079" end="00:35:49.119" region="r8" style="s1">
<span style="s2" tts:backgroundColor="black">Hello I am a fireman. Good morning</span>
<br/>
<span style="s2" tts:backgroundColor="black">Why do you </span>
<span style="s9" tts:backgroundColor="black">insist on that?</span>
</p>

我试着像一样输出

Hello I am a fireman. Good morning
Why do you insist on that?

我已经尝试过了,最终将其输出到一个文件中。

$xmlObject = simplexml_load_string($delivery, 'SimpleXmlElement', LIBXML_NOCDATA);
$xmlArray = json_decode(json_encode((array) $xmlObject), TRUE);
foreach($xmlArray['body']['div']['p'] as $p_tag) {
if (!is_string($p_tag['span'])) {
$multiLine = '';
foreach ($p_tag['span'] as $line) {
if (is_string($line)) {
$multiLine .= $line . "n";
}
}

$p_tag['span'] = $multiLine;
}
}
foreach($toPrint as $line) {

if (!isset($line['begin'])) {
continue;
}
$endSpace = '';
if (!$shrunk) {
$endSpace = '   ';
}
fwrite($fileOpen,"nn" . $line['begin'] . ' --> ' . $line['end'] . $endSpace . "n" . $line['content']);
}

然后逐行打印$p_tag,但它当然会产生

Hello I am a fireman. Good morning
Why do you 
insist on that?

从这里开始,我也尝试过

$value = $Dom->documentElement->nodeValue;
$lines = explode("n", $value);
$lines = array_map('trim', $lines); // remove leading and trailing whitespace
$lines = array_filter($lines); // remove empty elements

foreach($lines as $line) {
echo htmlentities($line);
}

但这会产生类似的东西

Hello I am a fireman.Good morningWhy do youinsist on that?

当我var_dump$p_tag时,它会产生类似于以下的东西

["span"]=>
array(3) {
[0]=>
string(34) "'Hello I am a fireman. Good morning"
[1]=>
string(28) "Why do you "
[2]=>
string(28) "insist on that?"
}
["br"]=>
array(0) {
}

所以break被打乱了顺序,所以我在查看XML对象时不能依赖它。跨距是分组的,换行符在一个单独的位置,所以在这种情况下,无法将换行符放在原始字符串中的位置。

您可以使用DOMdocument,搜索段落标记并循环其中的节点。

$string='<p begin="00:35:47.079" end="00:35:49.119" region="r8" style="s1">
<span style="s2" tts:backgroundColor="black">Hello I am a fireman. Good morning</span>
<br/>
<span style="s2" tts:backgroundColor="black">Why do you </span>
<span style="s9" tts:backgroundColor="black">insist on that?</span>
</p>';
$result=[];
$doc= new DOMdocument();
$doc->loadHTML($string);
//get all paragraphs by <p>
$par_tag = $doc->getElementsByTagName('p');
//loop all found paragraphs
foreach($par_tag as $par){
//loop the childnodes inside the paragraph    
foreach($par->childNodes as $child){
//get the nodename of the element
$tag = $child->nodeName;
//if it is <span>: get the text
if($tag==='span')$result[]=$child->nodeValue;

//if it is <br>, add a linefeed
else if($tag==='br')$result[]="n";
}
}

echo implode('',$result);

最新更新