XML – split only first occurrence


I want to split values from this XML

        <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>

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.




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




Add a Comment