I'm trying to extract a value from a span from a url of a site but I can not isolate this particular value ....
here is the span in question
<span data-currency-market="" data-usd="63968175026.0">
I want the data-usd value only
$html1 = file_get_contents( 'https://url.com' );
$dom1 = new DOMDocument();
@$dom1->loadHTML( $html1 );
foreach ($dom1->getElementsByTagName('span') as $tag) {
echo $tag->nodeValue . '<br/>';
}
You can use preg_match_all like this
<?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>";
?>
Will output this:
If all you need are the numbers juste use
print_r($output_array[1]);
So at the end, all you need is 2 lines of code
$html1 = file_get_contents( $string );
preg_match_all('/usd="(.*)"/', $html1, $output_array);
You can use
foreach($output_array[1] as $key=>$value){
echo $value;
}
to retreive the values
If you expect only one match in that page, you can use preg_match the same way like this instead of 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];
?>
Will output: 63968175026.0