XML – split only first occurrence

问题内容:

I want to split values from this 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>

To to “Rozmery:” and “42 x 64 x 36mm” … without any HTML element. But sometimes is there “Pomer strán: 16:4” and I still need only “Pomer strán:” and “16:4

Is there any way do this with PHP function ?

Thank you.

问题评论:

答案:

答案1:

I assume, that you already get your inner xml somehow (not stated in OP).
All you need, is a simple RegEx, matching everything up to the colon and everything behind.

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)

You could also split the inner XML by colon:

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

// $parts[0]: Contains before colon
// $parts[1]: Contains after colon

答案评论:

原文地址:

https://stackoverflow.com/questions/47755774/xml-split-only-first-occurrence

Add a Comment