0

I apologize to post this question, but I am totally incompetent in regards to regular expressions.

I received some deprecated code, in particular, this snippet:

eregi("<MERCHANT>(.*)<\/MERCHANT>", $fcontents, $merchant_id);
eregi("<ORDERID>(.*)<\/ORDERID>", $fcontents, $orderid_id);
eregi("<TXORDER>(.*)<\/TXORDER>", $fcontents, $txorder_id);
eregi("<AMOUNT>(.*)<\/AMOUNT>", $fcontents, $amount);
eregi("<RESULT>(.*)<\/RESULT>", $fcontents, $judge_re);
eregi("<CODE>(.*)<\/CODE>", $fcontents, $error_code);

It seems the eregi() function is deprecated as of PHP 5.3.

The input string is an XML string that's returned through cURL:

<?xml version="1.0" encoding="UTF-8" ?><DRAWBACKAPI><MERCHANT>10023951776</MERCHANT><ORDERID>1687143935</ORDERID><TXORDER>20141021114751</TXORDER><AMOUNT>0.01</AMOUNT><RESULT>N</RESULT><CODE>不在可信任的IP之内</CODE></DRAWBACKAPI>

I searched around StackOverflow and it appears preg_match() is the alternative to this deprecated function, but I am totally incompetent in regex to make this work - I appreciate any help.

theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169

1 Answers1

2

I would use an XML parser for this, becuase it's what they're built for.

<?php

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" ?><DRAWBACKAPI><MERCHANT>10023951776</MERCHANT><ORDERID>1687143935</ORDERID><TXORDER>20141021114751</TXORDER><AMOUNT>0.01</AMOUNT><RESULT>N</RESULT><CODE>不在可信任的IP之内</CODE></DRAWBACKAPI>
XML;

$objXml = simplexml_load_string($xml);
$merchant = $objXml->MERCHANT;
$orderid = $objXml->ORDERID;
//...
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49