简体   繁体   English

十进制数,逗号或第一个数字后的点

[英]Decimal number with comma or point after first number

EDIT 2 编辑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 . “ 7,49”我将PHP脚本自动转换为。

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. 这将回显7,4,9而不是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. 因此显示为499,而4大于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. 它将回显499。我希望它在4到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,>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,><,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. 因此,它将抓取页面上的所有价格,但也将回显html代码。 It splits the html code instead of only the price. 它拆分html代码而不是仅拆分价格。

Below is the HTML code on the website 以下是网站上的HTML代码

<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 谢谢

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.) (如果您对默认的浮动格式感到满意。)

Try this, add strip_tags it remove any HTML tags: 试试这个,添加strip_tags它删除所有HTML标签:

$price = strip_tags($oNode['div.item-prijs']);
$new_price = substr(chunk_split($price, 1, ','), 0, -1); 
echo $new_string;

Given that it is phpQuery you could use: 鉴于它是phpQuery,您可以使用:

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. 在这种情况下,它将生成一个数组,其中包含两个元素4和99。

UPDATE : 更新

To print the price in the way you wanted, use: 要以所需方式打印价格,请使用:

echo $price[0] . '.' . $price[1];

UPDATE 2 : 更新2

The phpQuery selection should perhaps look something like this: phpQuery选择应该看起来像这样:

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. 这将迭代每个div.item-prijs并将价格提取到数组$ price中。

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. 但是您也可以在代码中解析<sup>标记。

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 [0]是逗号前的数字数组

$numbers[1] is an array of number after comma $ numbers [1]是逗号后的数字数组

MAybe this helps; 也许这会有所帮助;

$string = $oNode['div.item-prijs']; 
$new_string = substr(chunk_split($string, 4, ','), 0, -1);
echo str_replace("<,/sup,>,", "", $new_string);

If i understand the problem correctly, you could strip the tags, multiply the price by 10 and then use number_format . 如果我正确理解问题,则可以剥离标签,将价格乘以10,然后使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM