Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何去除字符串中的空白,php_Php - Fatal编程技术网

如何去除字符串中的空白,php

如何去除字符串中的空白,php,php,Php,所以,我试过使用trim和str_replace,但我就是想不出来。我在谷歌上搜索了一下,似乎什么都没用 这是我的密码: function convertcurrency($euro){ if (is_numeric($euro)) { $currency = file_get_contents("http://www.google.com/ig/calculator?hl=en&q=".$euro."EUR%3D%3FUSD"); $contents = array_m

所以,我试过使用trim和str_replace,但我就是想不出来。我在谷歌上搜索了一下,似乎什么都没用

这是我的密码:

function convertcurrency($euro){
if (is_numeric($euro)) {
    $currency = file_get_contents("http://www.google.com/ig/calculator?hl=en&q=".$euro."EUR%3D%3FUSD");
    $contents = array_map('trim', explode(" ", $currency));
    $getint = array_map('trim', explode("\"", $contents[3]));
    unset($getint[0]);
    $usdollar = implode(" ", $getint);
    echo "$euro Euro's is equal to $usdollar U.S. Dollars";
}
else{
    echo "$euro is not a number, please enter a number.";
}
}

convertcurrency(123123);

?>
谢谢

编辑:

很抱歉,我应该发布我的输出和预期输出

产出:123123欧元相当于162362.3美元 预计产出:123123欧元相当于162362.3美元


一旦我去掉了空白,我就可以使用money_format函数来正确显示它。

您需要使用正则表达式来替换所有文本

$string = preg_replace('/[\s]*/', '', $needle);

这应该对您有用。

只执行json_解码比分解数据更容易,尤其是当您有空格时,这在所有情况下都不起作用。使用json_解码代替爆炸。

这是谷歌货币转换器的问题。请尝试使用以下代码替换货币之间的空白:

$currency = preg_replace('/[^a-z0-9.]/', '', $currency);

希望这对您有所帮助:)

您在哪里观察到这些空白?在生成的HTML页面中?是否可以发布当前输出和预期输出?“似乎什么都不起作用”并不是一个真正具体的问题清单:)。你想要什么,最好是,你为什么想要它?现在发生了什么?等等,对不起,我已经添加了输出和预期输出,感谢您指出这一点!结果不是一个有效的json字符串。我不知道这一点,我对编码很陌生,但有人告诉我我不能使用json。不过我会试试的。谢谢。你可以在这里阅读更多关于json和如何使用它的信息。它还包含一些示例。过来看。别担心。如果你懂得如何使用正则表达式,它会非常强大。非常感谢你。我尝试了通过谷歌找到的所有替代品,只有你的有效!