XML - 仅在第一次出现时拆分

时间:2017-12-11 15:06:48

标签: php xml function split

我想从这个XML中拆分值

<SPEC>
<![CDATA[<ul>
        <li>Rozmery: 42 x 64 x 36mm</li>
        <li>Váha: 63g</li>
        <li>Farba: biela</li>
        <li>Výkon: 2 x 25w @ 8 ohmov</li>
        <li>Citlivosť: 98 dB</li>
        <li>Frekvenčný rozsah: 80Hz-22kHz</li>
    </ul>]]>
</SPEC>

要“ Rozmery:”和“ 42 x 64 x 36mm ”...没有任何HTML元素。但有时会出现“Pomertrán:16:4 ”,我仍然只需要“Pomertrán:”和“ 16:4 ”< / p>

 用PHP函数有没有办法做到这一点?

谢谢。

1 个答案:

答案 0 :(得分:0)

我假设你已经以某种方式获得了你的内部xml(在OP中未说明)。 您所需要的只是一个简单的RegEx,将所有内容与冒号和后面的一切相匹配。

preg_match_all('~(\w+):(.+)~', $yourInnerXML, $matches);

// $matches[0]: Contains all findings
// $matches[1]: Contains all findings of the first group (everything before the colon)
// $matches[2]: Contains all findings of the second group (everyting after the colon)

你也可以用冒号分割内部XML:

$parts = explode(':', $yourInnerXML);

// $parts[0]: Contains before colon
// $parts[1]: Contains after colon
相关问题