doutang1873 2019-03-03 10:26
浏览 65

PHP XML - 返回错误值

currently I'm working on one e-commerce project. I have XML full of products that I need to implement on the e-shop.

The problem is that my XML parser returns bad values, example: I have float 32 000, 00 and it parse only 32 to my php variable. The number that I'm parsing is somehow divided by thousand and I don't know why. Exactly same code just with XML file that has 32 000 works fine. Are they any convert setting that I need to set while initializing SimpleXML object?

Thanks for help.

  • 写回答

1条回答 默认 最新

  • douchunji1885 2019-03-03 10:37

    Lets look at this

    I have float 32 000, 00 and it parse

    No you have a string of "32 000, 00". So if we look at how PHP does type juggling

    We see a few examples like this:

    $foo = 5 * "10 Little Piggies"; // $foo is integer (50)

    So the value of these 2 multiplied is 50 or 5*10 the ten is parsed from "10 Little Piggies". PHP will apply the same rules to what you have above which results in 32. Because it's a string with those spaces PHP will do it almost exactly the same way as above.

    You can easily test this:

    echo floatval('32 000, 00');




    You may have to manually convert that to what it should be. One easy thing to do is just remove the spaces:

      echo floatval(str_replace([' ',','], ['','.'], '32 000, 00'));
      #output 32000
      echo floatval(str_replace([' ',','], ['','.'], '32 290,01'));
      #output 32290.01

    If I remember right some countries use the , as the . for decimal seperator. But I had to replace the , with . to get them to work... There may be some edge cases where this won't work, but without a bigger sample of data I can't say for sure.

    Where or why those spaces are in there, I have no idea. It would be better to remove them from the XML entirely, but that may not be possible.

    Without some actual code in the question, that's the best I can do.


    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码
  • ¥15 编写vba代码实现数据录入工作