Decimal number with comma or point after first number
質問
EDIT 2
Thank you all for your help! By fusing the answers and some other forum post I managed to solve it by:
$string = strip_tags($oNode['div.item-prijs']);
$array = str_split($string,1);
$arraytotal = ( $array[0] . ',' . $array[1] . $array[2] );
echo $arraytotal;
And shows the correct price now. "7,49" The PHP script I have automatic converts , to .
Sorry that I can't give more question answered markings. Case closed.
Come on guys
$price = strip_tags($oNode['div.item-prijs']);
$new_price = substr(chunk_split($price, 1, ','), 0, -1);
echo $new_price;
This will echo 7,4,9 and not 7,49. But this code is the best code so far. Someone any idea how to fix this?
Well I'm stuck at this for a while now..
I am parsing data from a website, and I want to get the price but on the website there is no comma or point between the prices. so it shows like 499, the 4 is bigger than 99.
When I do:
$price = $oNode['div.item-prijs'];
echo $price;
It will echo 499. I want it to add comma or point between 4 and 99.
I've tried:
$price = $oNode['div.item-prijs'];
$new_price = substr(chunk_split($price, 1, ','), 0, -1);
echo $new_string;
This will echo:
<,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,7,<,s,u,p,>,4,9,<,/,s,u,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,4,<,s,u,p,>,9,9,<,/,s,u,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,1,<,s,u,p,>,4,9,<,/,s,u,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,0,<,s,u,p,>,6,9,<,/,s,u,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,1,<,s,u,p,>,9,9,<,/,s,u,p,>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,/,s,u,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,1,<,s,u,p,>,4,9,<,/,s,u,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,2,<,s,u,p,>,0,8,<,/,s,u,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,1,<,s,u,p,>,4,9,<,/,s,u,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,3,<,s,u,p,>,4,9,<,/,s,u,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,6,<,s,u,p,>,1,8,<,/,s,u,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,3,<,s,u,p,>,9,9,<,/,s,u,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,4,<,s,u,p,>,2,9,<,/,s,u,p,>,<,/,p,>,<,/,d,i,v,><,d,i,v, ,c,l,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,2,<,s,u,p,>,7,9,<,/,s,u,p,>,<,/,p,>,<,/,d,i,v,>
And this:
$string = $oNode['div.item-prijs'];
$new_string = substr(chunk_split($string, 4, ','), 0, -1);
echo $new_string;
This will echo:
,
7,49<,/sup,>,
<,/p><,/div,>,
4,99<,/sup,>,
1,49<,/sup,>,
0,69<,/sup,>,
1,99<,/sup,>,
1,49<,/sup,>,
1,49<,/sup,>,
3,49<,/sup,>,
3,49<,/sup,>,
3,69<,/sup,>,
1,19<,/sup,>,
1,89<,/sup,>,
1,99<,/sup,>,
1,99<,/sup,>,
1,49<,/sup,>,
2,99<,/sup,>,
1,99<,/sup,>,
1,09<,/sup,>,
2,25<,/sup,>,
1,99<,/sup,>,
0,99<,/sup,>,
1,54<,/sup,>,
2,09<,/sup,>,
1,78<,/sup,>,
1,71<,/sup,>,
0,99<,/sup,>,
1,49<,/sup,>,
2,08<,/sup,>,
1,49<,/sup,>,
3,49<,/sup,>,
6,18<,/sup,>,
3,99<,/sup,>,
4,29<,/sup,>,
2,79<,/sup,>
So it will crab every price on the page, but also echo's html code. It splits the html code instead of only the price.
Below is the HTML code on the website
<div class="item-prijs">
<p>
<cufon class="cufon cufon-canvas" alt="4" style="width: 27px; height: 42px; ">
<canvas width="47" height="43" style="width: 47px; height: 43px; top: -1px; left: -2px; "></canvas>
<cufontext>4</cufontext>
</cufon>
<sup>
<cufon class="cufon cufon-canvas" alt="99" style="width: 24px; height: 20px; ">
<canvas width="35" height="21" style="width: 35px; height: 21px; top: -1px; left: -1px; ">
</canvas><cufontext>99</cufontext>
</cufon>
</sup>
</p>
</div>
What am I doing wrong? I've tried different ways, but no success. Can someone please help me out with the right code?
Thanks
解決
Try this, add strip_tags
it remove any HTML tags:
$price = strip_tags($oNode['div.item-prijs']);
$new_price = substr(chunk_split($price, 1, ','), 0, -1);
echo $new_string;
他のヒント
Whatever happened to The Simple Solution?
$major = intval($price / 100);
$minor = intval($price) % 100;
echo "The price is $major.$minor.\n";
Or even:
echo "The price is " . ($price / 100) ".\n";
(If you're happy with default float formatting.)
Given that it is phpQuery you could use:
echo trim ($oNode['div.item-prijs']->text ());
To get the actual data your after.
But this is probably closer to what you're after:
$price = array ();
foreach ($oNode['div.item-prijs cufontext'] as $n)
{
$price[] = (int )pq ($n)->text ();
}
It will produce an array with -- in this case -- two elements, 4 and 99.
UPDATE:
To print the price in the way you wanted, use:
echo $price[0] . '.' . $price[1];
UPDATE 2:
The phpQuery selection should perhaps look something like this:
foreach (pq ($oNode)->find ('div.item-prijs') as $price_node)
{
// we're iterating each <div /> class 'item-prijs'.
$price = array ();
foreach (pq ($price_node)->find ('cufontext') as $pn)
{
// iterate each <cufontext /> child node to div.item-prijs
$price[] = (int )pq ($n)->text ();
}
// we could add $price to an array or something, but we'll just echo for now.
echo $price[0] . '.' . $price[1] . "\n";
}
This will iterate each div.item-prijs and extract the price into an array, $price.
If i understand the problem correctly, you could strip the tags, multiply the price by 10 and then use number_format. After that you just need to strip the last digit.
php -r 'echo substr(number_format((499*10)), 0, -1);' => 4,99
php -r 'echo substr(number_format((599*10)), 0, -1);' => 5,99
php -r 'echo substr(number_format((5099*10)), 0, -1);' => 50,99
Maybe this will fix your problem;
$price = $oNode['div.item-prijs'];
$new_price = substr(chunk_split($price, 1, ','), 0, -1);
echo $new_price; // not $new_string; I think $new_string is an other var in your code
but you also parse your <sup>
tag in your code.
Could you please try;
$preg='#<div class="item-prijs"><p>(.*?)<sup>(.*?)</sup></p></div>#si';
preg_match_all($preg,$new_string,$number,PREG_SET_ORDER);
$numbers[0] is an array of number before comma
$numbers[1] is an array of number after comma
MAybe this helps;
$string = $oNode['div.item-prijs'];
$new_string = substr(chunk_split($string, 4, ','), 0, -1);
echo str_replace("<,/sup,>,", "", $new_string);