Use php and regex to extract data from xml

hi, im new to php and all that good stuff so im looking for a little help

i would like to extract the “Juh2afHJ5oq9Pla0AkNcGs==” part of the following:

<body xmlns=‘http://jabber.org/protocol/httpbind’><challenge xmlns=‘urn:ietf:params:xml:ns:xmpp-sasl’>Juh2afHJ5oq9Pla0AkNcGs==</challenge></body>

using php (and a regex) and then store it in a variable. any help would be much appreciated

Good eyes, thanks for that. I’ve updated the post to to correct those now.

The regex could just be:

<challenge.>(.)</challenge>

You can test it over at http://www.regular-expressions.info/javascriptexample.html

You could use preg_match to retrieve the intended result from the xml code.

The following code presumes that you have the xml code stored in a variable called $xml


$matches = preg_match('/<challenge.*>(.*)<\\/challenge>/', $xml);
$result = matches[1];

Other alternatives can be to use xml-based processing instead, which others around here will be able to advise on.

And the / in </challenge> should be escaped: <\/challenge>

Just a FYI you forgot the / to end the delimiter in your preg_match.

:rofl:

hi, thanks for the reply…
it works with the address given but php spits out an error unknown identifier
still, ill fiddle with it and see if i can get it working, thanks again

And you forgot… Ok I ran out.

me again, just wanted to share a different solution to the same problem.
regex works but strip tags works too:

$stripped = strip_tags($xmlstring);

will remove the surrounding xml in above example