简体   繁体   English

PHP中的str_replace似乎不起作用

[英]str_replace in PHP does not seem to work

I'm trying to get some data from our central bank, but can't work it out. 我正试图从中央银行获取一些数据,但无法解决。 (and ofc. they don't provide support...) (并且他们不提供支持......)

$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());

And the result I got for this is: 我得到的结果是:

<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>

Which is not very nice for processing. 这对处理来说不是很好。 So I used: 所以我用过:

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

And for some reason, the result is: 由于某种原因,结果是:

&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;

Which is more than weird. 这不仅仅是奇怪的。 I wanted to clear these lt and gt codes, but instead lost the <> parts from the output. 我想清除这些lt和gt代码,但是从输出中丢失了<>部分。 I wanted to get the data with simplexml, but it gives back false no matter which output I give to it. 我想用simplexml获取数据,但无论我给它输出什么,它都会返回false。 This is the code I would like to use, but now it just gives and error message, since the contend of $xml is "false"; 这是我想要使用的代码,但现在它只是提供和错误消息,因为$ xml的竞争是“假”;

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

  var_dump($xml);

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

(It actually gives the result XMLs in one line, I just tidied up a bit.) (它实际上将结果XML放在一行中,我只是整理了一下。)

Try this code snippet here 在此处尝试此代码段

<?php

ini_set('display_errors', 1);

$string = <<<HTML

<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>
</s:Body>
</s:Envelope>
HTML;
$xml= html_entity_decode($string);

$domDocument = new DOMDocument();
$domDocument->loadXML($xml);
$results=$domDocument->getElementsByTagName("Day");
foreach($results->item(0)->childNodes as $node)
{
    if($node instanceof DOMElement)
    {
        echo $node->nodeValue;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM