PHP中的str_replace似乎不起作用

时间:2017-05-25 16:29:19

标签: php xml str-replace

我正试图从我们的中央银行获取一些数据,但无法解决这个问题。 (并且他们不提供支持......)

$client = new SoapClient("http://www.mnb.hu/arfolyamok.asmx?wsdl",array('trace' => 1));
$xml = new SimpleXMLElement($client->GetCurrentExchangeRates()->GetCurrentExchangeRatesResult);
echo $client->GetCurrentExchangeRates()->GetCurrentExchangeRatesResult;
echo (string)htmlentities($client->__getLastResponse());

我得到的结果是:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetCurrentExchangeRatesResponse xmlns="http://www.mnb.hu/webservices/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <GetCurrentExchangeRatesResult>
    &lt;MNBCurrentExchangeRates&gt;
      &lt;Day date="2017-05-25"&gt;
        &lt;Rate unit="1" curr="AUD"&gt;204,7&lt;/Rate&gt;
        &lt;Rate unit="1" curr="BGN"&gt;157,51&lt;/Rate&gt;
        &lt;Rate unit="1" curr="BRL"&gt;83,66&lt;/Rate&gt;
        &lt;Rate unit="1" curr="CAD"&gt;204,06&lt;/Rate&gt;
        &lt;Rate unit="1" curr="CHF"&gt;282,21&lt;/Rate&gt;
        &lt;Rate unit="1" curr="CNY"&gt;39,89&lt;/Rate&gt;
        &lt;Rate unit="1" curr="CZK"&gt;11,65&lt;/Rate&gt;
        &lt;Rate unit="1" curr="DKK"&gt;41,4&lt;/Rate&gt;
        &lt;Rate unit="1" curr="EUR"&gt;308,06&lt;/Rate&gt;
        &lt;Rate unit="1" curr="GBP"&gt;355,61&lt;/Rate&gt;
        &lt;Rate unit="1" curr="HKD"&gt;35,17&lt;/Rate&gt;
        &lt;Rate unit="1" curr="HRK"&gt;41,48&lt;/Rate&gt;
        &lt;Rate unit="100" curr="IDR"&gt;2,06&lt;/Rate&gt;
        &lt;Rate unit="1" curr="ILS"&gt;76,56&lt;/Rate&gt;
        &lt;Rate unit="1" curr="INR"&gt;4,24&lt;/Rate&gt;
        &lt;Rate unit="1" curr="ISK"&gt;2,73&lt;/Rate&gt;
        &lt;Rate unit="100" curr="JPY"&gt;245,13&lt;/Rate&gt;
        &lt;Rate unit="100" curr="KRW"&gt;24,49&lt;/Rate&gt;
        &lt;Rate unit="1" curr="MXN"&gt;14,89&lt;/Rate&gt;
        &lt;Rate unit="1" curr="MYR"&gt;64,07&lt;/Rate&gt;
        &lt;Rate unit="1" curr="NOK"&gt;32,92&lt;/Rate&gt;
        &lt;Rate unit="1" curr="NZD"&gt;192,59&lt;/Rate&gt;
        &lt;Rate unit="1" curr="PHP"&gt;5,5&lt;/Rate&gt;
        &lt;Rate unit="1" curr="PLN"&gt;73,67&lt;/Rate&gt;
        &lt;Rate unit="1" curr="RON"&gt;67,7&lt;/Rate&gt;
        &lt;Rate unit="1" curr="RSD"&gt;2,51&lt;/Rate&gt;
        &lt;Rate unit="1" curr="RUB"&gt;4,88&lt;/Rate&gt;
        &lt;Rate unit="1" curr="SEK"&gt;31,68&lt;/Rate&gt;
        &lt;Rate unit="1" curr="SGD"&gt;197,91&lt;/Rate&gt;
        &lt;Rate unit="1" curr="THB"&gt;8,01&lt;/Rate&gt;
        &lt;Rate unit="1" curr="TRY"&gt;76,91&lt;/Rate&gt;
        &lt;Rate unit="1" curr="UAH"&gt;10,43&lt;/Rate&gt;
        &lt;Rate unit="1" curr="USD"&gt;274,03&lt;/Rate&gt;
        &lt;Rate unit="1" curr="ZAR"&gt;21,23&lt;/Rate&gt;
      &lt;/Day&gt;
    &lt;/MNBCurrentExchangeRates&gt;
  </GetCurrentExchangeRatesResult>
</GetCurrentExchangeRatesResponse>

处理效果不是很好。所以我用过:

$res = (string)htmlentities($client->__getLastResponse());
$res = str_replace("&lt;", "<", $res);
$res = str_replace("&gt;", ">", $res);
echo $res;

由于某种原因,结果是:

&lt;MNBCurrentExchangeRates&gt;
          &lt;Day date="2017-05-25"&gt;
            &lt;Rate unit="1" curr="AUD"&gt;204,7&lt;/Rate&gt;
            &lt;Rate unit="1" curr="BGN"&gt;157,51&lt;/Rate&gt;
            &lt;Rate unit="1" curr="BRL"&gt;83,66&lt;/Rate&gt;
            &lt;Rate unit="1" curr="CAD"&gt;204,06&lt;/Rate&gt;
            &lt;Rate unit="1" curr="CHF"&gt;282,21&lt;/Rate&gt;
            &lt;Rate unit="1" curr="CNY"&gt;39,89&lt;/Rate&gt;
            &lt;Rate unit="1" curr="CZK"&gt;11,65&lt;/Rate&gt;
            &lt;Rate unit="1" curr="DKK"&gt;41,4&lt;/Rate&gt;
            &lt;Rate unit="1" curr="EUR"&gt;308,06&lt;/Rate&gt;
            &lt;Rate unit="1" curr="GBP"&gt;355,61&lt;/Rate&gt;
            &lt;Rate unit="1" curr="HKD"&gt;35,17&lt;/Rate&gt;
            &lt;Rate unit="1" curr="HRK"&gt;41,48&lt;/Rate&gt;
            &lt;Rate unit="100" curr="IDR"&gt;2,06&lt;/Rate&gt;
            &lt;Rate unit="1" curr="ILS"&gt;76,56&lt;/Rate&gt;
            &lt;Rate unit="1" curr="INR"&gt;4,24&lt;/Rate&gt;
            &lt;Rate unit="1" curr="ISK"&gt;2,73&lt;/Rate&gt;
            &lt;Rate unit="100" curr="JPY"&gt;245,13&lt;/Rate&gt;
            &lt;Rate unit="100" curr="KRW"&gt;24,49&lt;/Rate&gt;
            &lt;Rate unit="1" curr="MXN"&gt;14,89&lt;/Rate&gt;
            &lt;Rate unit="1" curr="MYR"&gt;64,07&lt;/Rate&gt;
            &lt;Rate unit="1" curr="NOK"&gt;32,92&lt;/Rate&gt;
            &lt;Rate unit="1" curr="NZD"&gt;192,59&lt;/Rate&gt;
            &lt;Rate unit="1" curr="PHP"&gt;5,5&lt;/Rate&gt;
            &lt;Rate unit="1" curr="PLN"&gt;73,67&lt;/Rate&gt;
            &lt;Rate unit="1" curr="RON"&gt;67,7&lt;/Rate&gt;
            &lt;Rate unit="1" curr="RSD"&gt;2,51&lt;/Rate&gt;
            &lt;Rate unit="1" curr="RUB"&gt;4,88&lt;/Rate&gt;
            &lt;Rate unit="1" curr="SEK"&gt;31,68&lt;/Rate&gt;
            &lt;Rate unit="1" curr="SGD"&gt;197,91&lt;/Rate&gt;
            &lt;Rate unit="1" curr="THB"&gt;8,01&lt;/Rate&gt;
            &lt;Rate unit="1" curr="TRY"&gt;76,91&lt;/Rate&gt;
            &lt;Rate unit="1" curr="UAH"&gt;10,43&lt;/Rate&gt;
            &lt;Rate unit="1" curr="USD"&gt;274,03&lt;/Rate&gt;
            &lt;Rate unit="1" curr="ZAR"&gt;21,23&lt;/Rate&gt;
          &lt;/Day&gt;
        &lt;/MNBCurrentExchangeRates&gt;

这不仅仅是奇怪的。我想清除这些lt和gt代码,但是丢失了&lt;&gt;输出中的零件。我想用simplexml获取数据,但无论我给它输出什么,它都会返回false。这是我想要使用的代码,但现在它只是给出了错误消息,因为$ xml的争用是“假”;

$xml = simplexml_load_string( (string)$res );

  var_dump($xml);

  foreach($xml->xpath('//Day') as $header) {
    echo (string)$header->Rate;
  }

(它实际上将结果XML放在一行中,我只是整理了一下。)

1 个答案:

答案 0 :(得分:0)

Try this code snippet here

In [267]: df = pd.DataFrame(index=range(5), columns=range(10))
     ...: S = pd.Series(np.random.randint(0,9,(18)))
     ...: df[1] = S
     ...: 

In [268]: df
Out[268]: 
     0  1    2    3    4    5    6    7    8    9
0  NaN  3  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
1  NaN  3  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
2  NaN  1  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
3  NaN  0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN
4  NaN  6  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN

In [269]: df = df.astype('object')
     ...: df[:] = df.iloc[:,[1]]
     ...: 

In [270]: df
Out[270]: 
   0  1  2  3  4  5  6  7  8  9
0  3  3  3  3  3  3  3  3  3  3
1  3  3  3  3  3  3  3  3  3  3
2  1  1  1  1  1  1  1  1  1  1
3  0  0  0  0  0  0  0  0  0  0
4  6  6  6  6  6  6  6  6  6  6