Question

I'm kind of stuck capturing a group with preg_match() in php.

This is my pattern:

<ns2:uniqueIds>(.*)<\/ns2:uniqueIds>

And this is the source:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body><ns2:ListResponse xmlns:ns2="http://censored"><ns2:uniqueIds>censored</ns2:uniqueIds><ns2:uniqueIds>censored</ns2:uniqueIds></ns2:ListResponse></env:Body></env:Envelope>

What am I missing?

Was it helpful?

Solution

While I agree with the people above that tell you to not use regular expressions to parse XML, I can see why you are doing it to just grab some value. This is how I managed to make it work:

PHP interpreter example:

php > $s = '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body><ns2:ListResponse xmlns:ns2="http://censored"><ns2:uniqueIds>censored</ns2:uniqueIds><ns2:uniqueIds>censored</ns2:uniqueIds></ns2:ListResponse></env:Body></env:Envelope>';
php > $p = '#<ns2:uniqueIds>(.*?)<\/ns2:uniqueIds>#i';
php > $a = array();
php > preg_match($p, $s, $a);
php > print_r($a);
Array
(
    [0] => <ns2:uniqueIds>censored</ns2:uniqueIds>
    [1] => censored
)

The only changes that I've made to your pattern is that I've used the lazy quantifier *? instead of just * (which is greedy).

Sources:

OTHER TIPS

You should probably use the native SoapClient instead to make it easier to both invoke the WebService as well as get the result as a PHP native datatype..

Using regular expressions to parse XML or HTML is usually a (very) bad idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top