0

I've created a CURL to get value from my other page. The result of CURL is a number and text like this

14.391,47 Rupiah Indonesia

How to get only 14.391,47 only without any text "Rupiah Indonesia"? I just need 14.391,47 so I can use it to math with other number

This what I already tried

$url ='https://www.google.com/search?q=1+usd+to+idr';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
$curl_response = curl_exec($curl);
curl_close($curl);
if(!preg_match_all("/<div[^>]*class=\"\s*BNeawe\s+iBp4i\s+AP7Wnd\s*\"[^>]*>(.*?)<\/div>/s", $curl_response, $result, PREG_SET_ORDER)){
   echo 'Invalid output';
   exit;
}
print_r($curl_response);
// putting data to array
foreach($result as $item){
   $key=$item[1];
}
echo '<pre>';
print_r((float) filter_var( trim($key), FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ));
echo '</pre>';

but the result always 4714.39147 should be 14.391,47

dogemaster
  • 25
  • 5
  • 3
    Does this answer your question? [Remove non-numeric characters (except periods and commas) from a string](https://stackoverflow.com/questions/4949279/remove-non-numeric-characters-except-periods-and-commas-from-a-string) – glovemobile Mar 13 '21 at 02:12
  • NO. that's not the answer, see my result of questions always return 4714.39147, if I use your suggest to always return 4714.391,47 – dogemaster Mar 14 '21 at 02:09

4 Answers4

2

Get all the numbers before 'Indonesische roepia' with a regex:

/([0-9.,]+)\ Indonesische roepia/
<?php

$url ='https://www.google.com/search?q=1+usd+to+idr';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$curl_response = curl_exec($curl);
curl_close($curl);
if(!preg_match_all("/<div[^>]*class=\"\s*BNeawe\s+iBp4i\s+AP7Wnd\s*\"[^>]*>(.*?)<\/div>/s", $curl_response, $result, PREG_SET_ORDER)){
   echo 'Invalid output';
   exit;
}

$re = '/([0-9.,]+)\ Indonesische roepia/';
preg_match($re, $result[0][1], $number);
echo '<pre>';
print_r((float) $number[1]);
echo '</pre>';
<pre>14.391</pre>
0stone0
  • 34,288
  • 4
  • 39
  • 64
1

You can use a simple regex.

$re = '/[0-9\.\,]+/';
$str = '14.391,47 Rupiah Indonesia';

preg_match($re, $str, $matches);

var_dump($matches);


/*
array(1) {
  [0]=>
  string(9) "14.391,47"
}
*/
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
1

You could take only the part before the (space).

$str = '14.391,47 Rupiah Indonesia';
$num = substr($str, 0, strpos($str, ' '));
echo $num; // 14.391,47
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
1
<?php
$uang= '14.391,47 Rupiah Indonesia';
echo preg_replace('/[^0-9,.]+/', '', $uang); //14.391,47
glovemobile
  • 302
  • 2
  • 8
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Mar 13 '21 at 19:06