使用php从网站中提取特定数据

使用php从网站中提取特定数据,php,extract,Php,Extract,我想从某个网站获取数据。 Html的一部分如下所示: 10750.00 我想在没有逗号和点的情况下退出10750 10750.00只是一个例子。此数字从以下位置开始更改: <span class="last">anything here</span> 这将只提取“任何内容”,并删除其他不必要的HTML 然后: 这将删除HTML标记,或者,如果您想手动尝试 最后 $page = str_replace(['.', ','], '' , $page); 删除点和逗号 删除

我想从某个网站获取数据。 Html的一部分如下所示:

10750.00

我想在没有逗号和点的情况下退出10750

10750.00只是一个例子。此数字从以下位置开始更改

<span class="last">anything here</span>
这将只提取“任何内容”,并删除其他不必要的HTML

然后:

这将删除HTML标记,或者,如果您想手动尝试

最后

$page = str_replace(['.', ','], '' , $page);
删除点和逗号

删除小数

您可以强制转换为int,即使使用该数字进行计算时PHP会自动处理


Fiddle here==>

如果变量中包含HTML部分,则可以执行以下操作:

$html = '<span class="last">10,750.00</span>';
$without_tag = strip_tags($html); // Remove Tags HTML
$number_float = (float) str_replace(',', '', $without_tag); // Remove commas and change to float
echo $number_float; // 10750
$html='10750.00';
$without_tag=strip_tags($html);//删除HTML标记
$number_float=(float)str_replace(',','',$,不带标记);//删除逗号并更改为浮动
echo$number_float;//10750
如果您没有HTML,可以使用:

$html = file_get_contents("http://example.com/");
$part = stristr(stristr($html, '<span class="last">'), '</span>', true);
$without_tag = strip_tags($part); // Remove Tags HTML
$number_float = (float) str_replace(',', '', $without_tag); // Remove commas and change to float
echo $number_float; // 10750
$html=文件获取内容(“http://example.com/");
$part=stristr(stristr($html',),'',true);
$without_tag=strip_tags($part);//删除HTML标记
$number_float=(float)str_replace(',','',$,不带标记);//删除逗号并更改为浮动
echo$number_float;//10750

如果您知道它是10750.00,为什么需要提取它?除非您可以访问同一台服务器上的页面,否则需要使用Javascript/jquery进行提取。您可以下载带有文件\u get\u contents()的HTML,然后使用正则表达式查找值,最后进行str\u替换以删除点和逗号。然而,HTML上的正则表达式有时会有问题。如果它是一个小的HTML文件,它可能工作得很好,数字10750.00只是一个例子。数字在变化。我想捕捉它,用在一些等式中。数字10750.00只是一个例子。数字在变化。我想把它捕捉下来,用在一些等式中,我得到的是空白页。我看不到任何回声我怎么能去掉逗号和点呢?看起来也是这样:10750对不起,我不懒,只是个新手:)如果你接受这个答案就好了!谢谢您可以在这里找到方法==>
$page = str_replace($toSearch1, "", $page);
$page = str_replace($toSearch1, "", $page);
$page = str_replace(['.', ','], '' , $page);
$page = substr($page, 0, -2);
$page = (int)$page;
$html = '<span class="last">10,750.00</span>';
$without_tag = strip_tags($html); // Remove Tags HTML
$number_float = (float) str_replace(',', '', $without_tag); // Remove commas and change to float
echo $number_float; // 10750
$html = file_get_contents("http://example.com/");
$part = stristr(stristr($html, '<span class="last">'), '</span>', true);
$without_tag = strip_tags($part); // Remove Tags HTML
$number_float = (float) str_replace(',', '', $without_tag); // Remove commas and change to float
echo $number_float; // 10750