-1

I have the following code that I think it should work because is REGEX correct (works under Ruby and is checked on regex101.com):

if (preg_match('/<Approved>/', $response)) {
  preg_match('/<Approved>(.*)<\/Approved>/', $response, $matches);
  $app_code = $matches[0];
}

but I get:

PHP Warning:  preg_match(): Unknown modifier '('

and the match that should return APPROVED is not matched. The searched data is a XML like:

<Approved>APPROVED</Approved>

or

<Approved>DECLINED</Approved>

EDIT:

More code as per request:

curl_close($ch);

var_dump($response);

if (preg_match('/<Approved>(.*)<\/Approved>/', $response)) {
  preg_match('/<Approved>(.*)<\/Approved>/', $response, $matches);
  $app_code = $matches[0];
}

if (preg_match('<ReturnCode>(.*)<\/ReturnCode>', $response)) { // This is the problem line
  preg_match('/<ReturnCode>(.*)<\/ReturnCode>/', $response, $matches);
  $retcode = $matches[0];
}
bsteo
  • 1,738
  • 6
  • 34
  • 60
  • 3
    Are you sure this is the regexp that fails? It looks fine to me. – Halcyon Sep 08 '15 at 14:45
  • 1
    works fine for me also – Halayem Anis Sep 08 '15 at 14:46
  • what code is above if condition.? paste it – Nana Partykar Sep 08 '15 at 14:48
  • Yes, I'm sure, and is a simple regex. – bsteo Sep 08 '15 at 14:48
  • Since you appear to be trying to parse XML using regex, may I suggest that you (a) consider using an XML parser (PHP has suitable XML classes built-in), and (b) read [this canonical answer](http://stackoverflow.com/a/1732454/3913358) about why you shouldn't parse HTML/XML with regex. – Simba Sep 08 '15 at 15:23
  • @Simba, you are right, but my data is not canonical XML, no root whatsoever, that's why I use regex. – bsteo Sep 08 '15 at 15:54
  • Instead of using 2 preg_match's you could just use one like the following if ( preg_match('/\(.*)\<\/approved\>/i', $data, $matches) ) { – TURTLE Sep 08 '15 at 16:14
  • Fair enough. Although if that's the only non-XML conformant point, you could add a root element -- `$xml = "{$xml}";` and then parse it. – Simba Sep 08 '15 at 16:16

1 Answers1

0

Sorry, my bad... I forgot to put delimiters on a line... The code should be:

curl_close($ch);

var_dump($response);

if (preg_match('/<Approved>(.*)<\/Approved>/', $response)) {
  preg_match('/<Approved>(.*)<\/Approved>/', $response, $matches);
  $app_code = $matches[1];
}

if (preg_match('/<ReturnCode>(.*)<\/ReturnCode>/', $response)) { // This was the problem lines, forgot the delimiters
  preg_match('/<ReturnCode>(.*)<\/ReturnCode>/', $response, $matches);
  $retcode = $matches[1];
}

And then works.

Anyway, a better approach as user @Simba commented is to use the built-in PHP XML parser here:

<?php
$myXMLData =
'<?xml version="1.0" encoding="UTF-8"?>
<Result>
<Approved>APPROVED</Approved>
<ReturnCode>N:APPROVED</ReturnCode>
</Result>';

$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");

$approved = $xml->Approved;
echo $approved . "\n";
?>

Output: APPROVED

So my little snippet above should better be:

  if (preg_match('/Approved/', $response)) {
    $xml=simplexml_load_string($response) or die("XML Parse Error: Cannot create object");
    $app_code = $xml->Approved;
    $retcode = $xml->ReturnCode;
  }
bsteo
  • 1,738
  • 6
  • 34
  • 60