1

H there - firs time posting here - I will spare letting you know that I'm "new to PHP and Perl", as that will become painfully obvious below.

Below is a script I wrote to scrape a bank's asset amount, multiply it by 3% and display the results. I explain my flow idea below, and show the result I am getting compared to what I should be getting. My guess is that I can't do math with a value that is still inside an array. Is there an implode process I'm missing before I can start doing some math.

The URL I'm scraping is on my server so run at will.

<?php  

// scrape page content of credit union
$dataassets = file_get_contents('http://ablistings.com/boise-project.htm');

// define regular expression to grab credit union's assets amount
$regexassets = '/members,\s(.+?)\smillion/';

// run the preg_match
preg_match($regexassets,$dataassets,$matchassets);

// print the second key of the array [1] which is just the numerical value (in this case $214.4)
$assetsamount = print_r($matchassets[1]);

// add 3% to the value of $214.4, so first we multiple .03 by value
$three_percent_increase = ($assetsamount * .03); // should egual 6.432

// now add the percentage to the orginal asset amount - in this case 6.432 + 214.4 which = 220.832
$final_sum = ($three_percent_increase + $assetsamount);

echo $final_sum;
// sum should be $220.832 but the sum this script produces is $214.41.03 

?> 
4

1 回答 1

0
<?php
$dataassets = 'members, $214.40 million';
// define regular expression to grab credit union's assets amount
$regexassets = '/members,\s\$([\d.]+?)\smillion/';

// run the preg_match
preg_match($regexassets,$dataassets,$matchassets);

// print the second key of the array [1] which is just the numerical value (in this case $214.4)
$assetsamount = $matchassets[1];

var_dump($assetsamount);
// add 3% to the value of $214.4, so first we multiple .03 by value
$three_percent_increase = ($assetsamount * .03); // should egual 6.432
var_dump($three_percent_increase);
// now add the percentage to the orginal asset amount - in this case 6.432 + 214.4 which = 220.832
$final_sum = ($three_percent_increase + $assetsamount);

echo $final_sum;

您的代码有 2 个问题:

  1. 您没有跳过$原始金额。我已经修改了正则表达式来做到这一点。

  2. 您将结果分配给print_rto $assetsamountprint_r不返回它打印的值,它只是返回1. 所以它在 1 上增加了 3%,结果是1.03.

当它打印出来$214.41.03时,实际上是两件事:214.4来自print_r($matchassets[1])1.03来自echo $final_sum

于 2013-10-26T00:46:21.347 回答