带逗号的小数位数或第一个数字后的点数

时间:2011-11-24 13:43:58

标签: php string parsing phpquery

编辑2

谢谢大家的帮助! 通过融合答案和其他一些论坛帖子,我设法通过以下方式解决:

$string = strip_tags($oNode['div.item-prijs']);
$array = str_split($string,1); 
$arraytotal = ( $array[0] . ',' . $array[1] . $array[2] );
echo $arraytotal;

现在显示正确的价格。 “7,49”我自动转换为的PHP脚本。

很抱歉,我无法提供更多问题回答标记。案件已结案。

加油人

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

这将回显7,4,9而不是7,49。但是这段代码是目前为止最好的代码。 有人知道如何解决这个问题吗?

我现在已经坚持了一段时间..

我正在从网站解析数据,我想得到价格,但在网站上没有逗号或价格之间的点。因此它显示为499,4大于99。

当我这样做时:

$price = $oNode['div.item-prijs'];
echo $price;

它将回应499。 我希望它添加逗号或4到99之间的点。

我试过了:

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

这将回应:

<,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,=,",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,a,s,s,=,",i,t,e,m,-,p,r,i,j,s,",>,<,p,>,0,<,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,>,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,>

而且:

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

这将回应:

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

所以它会扼杀页面上的每个价格,但也会回显html代码。它拆分html代码而不是价格。

以下是网站上的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>

我做错了什么?我尝试了不同的方法,但没有成功。有人可以用正确的代码帮助我吗?

由于

5 个答案:

答案 0 :(得分:5)

简单解决方案发生了什么?

$major = intval($price / 100);
$minor = intval($price) % 100;

echo "The price is $major.$minor.\n";

甚至:

echo "The price is " . ($price / 100) ".\n";

(如果您对默认的浮动格式感到满意。)

答案 1 :(得分:2)

试试这个,添加strip_tags它会删除所有HTML标记:

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

答案 2 :(得分:2)

鉴于它是phpQuery你可以使用:

echo trim ($oNode['div.item-prijs']->text ());

获取实际数据。

但这可能更接近你所追求的目标:

$price = array ();
foreach ($oNode['div.item-prijs cufontext'] as $n)
{
        $price[] = (int )pq ($n)->text ();
}

它将产生一个数组 - 在这种情况下 - 两个元素,4和99。

<强>更新

要按照您想要的方式打印价格,请使用:

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

更新2

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";
}

这将迭代每个div.item-prijs并将价格提取到数组$ price。

答案 3 :(得分:1)

也许这会解决你的问题;

$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

但您也会在代码中解析<sup>标记。

你能试试吗?

$preg='#<div class="item-prijs"><p>(.*?)<sup>(.*?)</sup></p></div>#si'; 
preg_match_all($preg,$new_string,$number,PREG_SET_ORDER); 

$ numbers [0]是逗号之前的数字数组

$ numbers [1]是逗号后的数字数组

MAybe这有帮助;

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

答案 4 :(得分:1)

如果我正确理解了问题,您可以剥离标记,将价格乘以10,然后使用number_format。之后你只需要删除最后一位数字。

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