Jump to content

Recommended Posts

Hello!

 

I am working on a script to fetch a price of metal, remove the dollar sign, and then multiple or add it by a number.  My current code I created is as follows:

 

<?php

 

$file = file_get_contents("http://www.apmex.com");

$do = preg_match('/<span id="ctl08_ctl05_gvMetals_ctl02_label3">(.*)<\/span>/', $file, $matches);

if ($do = true) {

 

$no_dollar = preg_replace('/[\$]/', '', $matches);

 

print $no_dollar['1'];

 

} else {

 

echo "No Price";

}

 

?>

 

I got the script to pull a current value and then remove the dollar sign, however I need to multiply it by a value (Say, 1.05).  I've tried for several days without anything more than a 0 result.  Any suggestions?

 

Thank you so much,

Jeremy  :)

Link to comment
https://forums.phpfreaks.com/topic/230444-web-fetch-remove-multiply-variable/
Share on other sites

before calculate with price you must strip out comma too

change regex pattern in preg replace from [\$] to [\$,]

<?php

$file = file_get_contents("http://www.apmex.com");
$do = preg_match('/<span id="ctl08_ctl05_gvMetals_ctl02_label3">(.*)<\/span>/', $file, $matches);
if ($do = true) {

$no_dollar = preg_replace('/[\$,]/', '', $matches);

   print $no_dollar['1'];

} else {

   echo "No Price";
}
echo  "\n<hr />Price multypled with 1.7 is ",$no_dollar[1]*1.7;
?>

@ Crayon Violent and sasa, thank you so much!  I've been spending the past days pulling my hair out trying to figure this out.  I got it to work with multiplication and addition, but how do I get it to show as such:  $1,111.11.  Where it has a dollar sign, comma, and only two denominators?

 

Thank you so much for getting me to this point!

Jeremy

Hello,

 

Using the numbers format I got to the following which doesn't work:

 

<?php

 

$file = file_get_contents("http://www.apmex.com");

$do = preg_match('/<span id="ctl08_ctl05_gvMetals_ctl02_label3">(.*)<\/span>/', $file, $matches);

if ($do = true) {

 

$no_dollar = preg_replace('/[\$,]/', '', $matches);

 

print $no_dollar['1'];

 

$dollar = $no_dollar['1']*1.02;

 

// english notation without thousands seperator

$english_format_number = number_format($dollar, 2, '.', ',');

// 1234.57

  print $english_format_number['1'];

 

} else {

 

  echo "No Price";

}

echo  "\n<hr />Price multiplied with 1.02 is ",$no_dollar[1]*1.02;

?>

 

Thank you,

Jeremy

 

try

<?php
$file = file_get_contents("http://www.apmex.com");
$do = preg_match('/<span id="ctl08_ctl05_gvMetals_ctl02_label3">(.*)<\/span>/', $file, $matches);
if ($do) {
    $no_dollar = preg_replace('/[\$,]/', '', $matches);
    $dollar = $no_dollar['1']*1.02;
    echo  "Price multiplied with 1.02 is ",  number_format($dollar,2,'.',',');
} else {
    echo "No Price";
}
?>

  • 3 weeks later...

Hi jskarr1982,

 

I'm working on a website which is very similar with you. I just need the silver spot price but i cant seem to get the codes correct. Anyone can help me out abit here ? im losing hair fast >.<

 

<?php

$file = file_get_contents("http://www.apmex.com/Category/503/Silver.aspx");

$do = preg_match('/<span id="ctl10_ctl00_MetalInfo1_lblAsk">(.*)<\/span>/', $file, $matches);

if ($do) {

    $no_dollar = $matches;

    $dollar = $no_dollar['1'];

    echo $dollar;

} else {

    echo "No Price";

}

?>

<?php echo $dollar; ?>

 

I'm get No Price output but i had double check the span id and the url and the source code...its all correct =(

 

Thanks,

Victor

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.