删除“ ”在 PHP 中

发布于 2024-12-12 12:54:41 字数 1734 浏览 0 评论 0原文

使用此网址 http://www.google.com/ig /calculator?hl=fr&q=150euro=?dirhams,我们可以进行货币换算。

这是结果: {lhs: "150 Euros",rhs: "1 691.50299 Moroccan dirhams",error: "",icc: true}

我尝试使用这个看起来像 json 的结果。 所以我使用 json_decode 但它不起作用。

所以我认为它看起来像序列化/反序列化,所以我使用 unserialize 但它不起作用。

紧张的是,我使用了正则表达式,最后我得到了带有   的迪拉姆值,因为该值在逗号之前有 4 个数字。

而这个 我无法删除它!我尝试 preg_replace、str_replace...没办法!

函数

这是我只想获取“迪拉姆值”的

,只有数字,作为浮点数任何人都可以帮助我吗?

public function convertDirhams($prix, $monnaie_base = 'euro', $monnaie_convert = 'dirhams')
{
    $prix = urlencode($prix);
    $monnaie_base = urlencode($monnaie_base);
    $monnaie_convert = urlencode($monnaie_convert);
    $url = "http://www.google.com/ig/calculator?hl=fr&q=$prix$monnaie_base=?$monnaie_convert";
    $ch = curl_init();
    $timeout = 0;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $rawdata = curl_exec($ch);
    curl_close($ch);
    /*$data = explode('"', $rawdata);
    $data = explode(' ', $data['3']);
    $var = $data['0'];*/

    $data = preg_replace('/ /','',$rawdata);
    $data = explode(':',$data);
    $data = preg_replace('/"/','',$data[2]);
    $data = preg_replace('/Moroccandirhams,error/isu','',$data);
    $prix_final = preg_replace("/&#?[a-z0-9]{2,8};/i","",$data);
    //echo $prix_final;


    return (float)trim($data);
} 

With this url http://www.google.com/ig/calculator?hl=fr&q=150euro=?dirhams, we can have a currency convert.

This is the result : {lhs: "150 Euros",rhs: "1 691.50299 Moroccan dirhams",error: "",icc: true}

I try to work with this result which looks like json.
So I use json_decode but it doesn't work.

So I thought it looks like serilize/unserialize so I use unserialize but it doesn't work.

Nervous, I used regexp and at the end I've got my value in dirhams with an   because the value gets 4 numbers before the coma.

And this   I can't delete it ! I try preg_replace, str_replace... no ways !

This is the function

I just want to get the "dirhams value", only the numbers, as a float

Anyone can help me ?

public function convertDirhams($prix, $monnaie_base = 'euro', $monnaie_convert = 'dirhams')
{
    $prix = urlencode($prix);
    $monnaie_base = urlencode($monnaie_base);
    $monnaie_convert = urlencode($monnaie_convert);
    $url = "http://www.google.com/ig/calculator?hl=fr&q=$prix$monnaie_base=?$monnaie_convert";
    $ch = curl_init();
    $timeout = 0;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $rawdata = curl_exec($ch);
    curl_close($ch);
    /*$data = explode('"', $rawdata);
    $data = explode(' ', $data['3']);
    $var = $data['0'];*/

    $data = preg_replace('/ /','',$rawdata);
    $data = explode(':',$data);
    $data = preg_replace('/"/','',$data[2]);
    $data = preg_replace('/Moroccandirhams,error/isu','',$data);
    $prix_final = preg_replace("/&#?[a-z0-9]{2,8};/i","",$data);
    //echo $prix_final;


    return (float)trim($data);
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽梦紫曦~ 2024-12-19 12:54:41

当我使用谷歌货币转换器时,我也遇到了这个问题。我尝试了很多事情,但没有任何对我有用。最后我找到了这段代码。

您可以尝试使用 :

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

$number = ereg_replace("[^A-Za-z0-9.]", "", $number );

两者都工作得很好。 ereg_replace() 是 php5.3 中已弃用的函数。

I too was stuck with this issue when i used google currency converter. I tried many things and nothing worked for me. Finally i found this piece of code.

You can try with :

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

or

$number = ereg_replace("[^A-Za-z0-9.]", "", $number );

Both works perfectly fine. ereg_replace() is depreciated function in php5.3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文