提取特定跨度
Extract a particular span
我正在尝试从站点的 url 的跨度中提取值,但我无法分离出该特定值....
这里是有问题的跨度
<span data-currency-market="" data-usd="63968175026.0">
我只想要data-usd值
$html1 = file_get_contents( 'https://url.com' );
$dom1 = new DOMDocument();
@$dom1->loadHTML( $html1 );
foreach ($dom1->getElementsByTagName('span') as $tag) {
echo $tag->nodeValue . '<br/>';
}
你可以这样使用preg_match_all
<?php
// Loading data for demo
$html1 = '[...]
<span data-currency-market="" data-usd="63968175026.0"></span>
<span data-currency-market="" data-usd="63968175026.0"></span>
<span data-currency-market="" data-usd="63968175026.0"></span>';
// Your data source
//$html1 = file_get_contents( $string );
preg_match_all('/usd="(.*)"/', $html1, $output_array);
// Showing array
echo "<pre>";
print_r($output_array);
echo "</pre>";
?>
将输出:
如果您只需要使用
的数字
print_r($output_array[1]);
所以最后,你只需要2行代码
$html1 = file_get_contents( $string );
preg_match_all('/usd="(.*)"/', $html1, $output_array);
您可以使用
foreach($output_array[1] as $key=>$value){
echo $value;
}
检索值
如果您希望该页面中只有一个匹配项,您可以像这样使用 preg_match 而不是 preg_match_all
<?php
$html1 = '[...]
<span data-currency-market="" data-usd="63968175026.0"></span>
<span data-currency-market="" data-cad="73175026.0"></span>
<span data-currency-market="" data-eur="83968176.0"></span>';
//$html1 = file_get_contents( $string );
preg_match('/usd="(.*)"/', $html1, $output_array);
echo $output_array[1];
?>
将输出:63968175026.0
要使用 DOM 正确执行此操作,您可以使用 XPath 查找所有具有 data-usd 属性的 span 元素。 XPath 只是 //span/@data-usd
,其中 @
表示一个属性。对 query()
returns 匹配节点列表的调用,因此您只需按照与 getElementsByTagName()
.
相同的方式循环
$html1 = '<div><span data-currency-market="" data-usd="1">
<span data-currency-market="" data-eur="2">
<span data-currency-market="" data-usd="3">
<span data-currency-market="" data-eur="4"></div>';
//$html1 = file_get_contents( 'https://url.com' );
$dom1 = new DOMDocument();
$dom1->loadHTML( $html1 );
$xp = new DOMXPath($dom1);
$dataUSD = $xp->query("//span/@data-usd");
foreach ($dataUSD as $tag) {
echo $tag->nodeValue . '<br/>';
}
其中有测试数据returns...
1<br/>3<br/>
我正在尝试从站点的 url 的跨度中提取值,但我无法分离出该特定值....
这里是有问题的跨度
<span data-currency-market="" data-usd="63968175026.0">
我只想要data-usd值
$html1 = file_get_contents( 'https://url.com' );
$dom1 = new DOMDocument();
@$dom1->loadHTML( $html1 );
foreach ($dom1->getElementsByTagName('span') as $tag) {
echo $tag->nodeValue . '<br/>';
}
你可以这样使用preg_match_all
<?php
// Loading data for demo
$html1 = '[...]
<span data-currency-market="" data-usd="63968175026.0"></span>
<span data-currency-market="" data-usd="63968175026.0"></span>
<span data-currency-market="" data-usd="63968175026.0"></span>';
// Your data source
//$html1 = file_get_contents( $string );
preg_match_all('/usd="(.*)"/', $html1, $output_array);
// Showing array
echo "<pre>";
print_r($output_array);
echo "</pre>";
?>
将输出:
如果您只需要使用
的数字print_r($output_array[1]);
所以最后,你只需要2行代码
$html1 = file_get_contents( $string );
preg_match_all('/usd="(.*)"/', $html1, $output_array);
您可以使用
foreach($output_array[1] as $key=>$value){
echo $value;
}
检索值
如果您希望该页面中只有一个匹配项,您可以像这样使用 preg_match 而不是 preg_match_all
<?php
$html1 = '[...]
<span data-currency-market="" data-usd="63968175026.0"></span>
<span data-currency-market="" data-cad="73175026.0"></span>
<span data-currency-market="" data-eur="83968176.0"></span>';
//$html1 = file_get_contents( $string );
preg_match('/usd="(.*)"/', $html1, $output_array);
echo $output_array[1];
?>
将输出:63968175026.0
要使用 DOM 正确执行此操作,您可以使用 XPath 查找所有具有 data-usd 属性的 span 元素。 XPath 只是 //span/@data-usd
,其中 @
表示一个属性。对 query()
returns 匹配节点列表的调用,因此您只需按照与 getElementsByTagName()
.
$html1 = '<div><span data-currency-market="" data-usd="1">
<span data-currency-market="" data-eur="2">
<span data-currency-market="" data-usd="3">
<span data-currency-market="" data-eur="4"></div>';
//$html1 = file_get_contents( 'https://url.com' );
$dom1 = new DOMDocument();
$dom1->loadHTML( $html1 );
$xp = new DOMXPath($dom1);
$dataUSD = $xp->query("//span/@data-usd");
foreach ($dataUSD as $tag) {
echo $tag->nodeValue . '<br/>';
}
其中有测试数据returns...
1<br/>3<br/>