-1

How to get data and set in to php var when use file_get_contents PHP ?

this is code for http://www.example.com/test.php

<table align="center">
    <thead style="background-color: #F0F0F0; color: #333333; text-align: center;">
        <tr>
           <th class="countryHeader">Country</th>
            <th class="intHeader">&nbsp;</th>
            <th colspan="2" class="intHeader">Desktop CPM</th>
            <th colspan="2" class="bannerHeader">Mobile CPM</th>
        </tr>
    </thead>
    <tbody>
        <tr class="breaker">
            <td>&nbsp;</td>
            <td>Highest</td>
            <td>Unique</td>
            <td>Raw</td>
            <td>Unique</td>
            <td>Raw</td>
        </tr>
        <tr class="odd rateVals">
            <td class="countryName">United States</td>
            <td class="cpmRaw">$2.00</td>
            <td class="cpm">$3.44</td>
            <td class="cpmRaw">$2.00</td>
            <td class="cpmMobile">$1.63</td>
            <td class="cpmMobileRaw">$0.97</td>
        </tr>
        <tr class="even rateVals">
            <td class="countryName">Canada</td>
            <td class="cpmRaw">$1.20</td>
            <td class="cpm">$2.52</td>
            <td class="cpmRaw">$1.20</td>
            <td class="cpmMobile">$1.88</td>
            <td class="cpmMobileRaw">$1.06</td>
        </tr>
        <tr class="odd rateVals">
            <td class="countryName">United Kingdom</td>
            <td class="cpmRaw">$1.60</td>
            <td class="cpm">$2.10</td>
            <td class="cpmRaw">$1.08</td>
            <td class="cpmMobile">$1.71</td>
            <td class="cpmMobileRaw">$0.94</td>
        </tr>
        <tr class="even rateVals">
            <td class="countryName">Australia</td>
            <td class="cpmRaw">$1.50</td>
            <td class="cpm">$2.62</td>
            <td class="cpmRaw">$1.20</td>
            <td class="cpmMobile">$2.73</td>
            <td class="cpmMobileRaw">$1.49</td>
        </tr>
        <tr class="breaker">
            <td colspan="6">&nbsp;</td>
        </tr>
        <tr class="odd rateVals">
            <td class="countryName">United Arab Emirates</td>
            <td class="cpmRaw">$1.48</td>
            <td class="cpm">$2.49</td>
            <td class="cpmRaw">$1.45</td>
            <td class="cpmMobile">$2.11</td>
            <td class="cpmMobileRaw">$1.42</td>
        </tr>
        <tr class="odd rateVals">
            <td>All other countries</td>
            <td>-</td>
            <td colspan="2">$0.25 - $2.00</td>
            <td colspan="2">$0.06 - $0.22</td>
        </tr>
    </tbody>
</table>

And this is code for my_code.php

<?php
$test = file_get_contents('http://www.example.com/test.php');
//echo $test;
?>

And i want to get php var from column Mobile CPM raw value like this

$United_States = "0.97";
$Canada = "1.07";
$United_Kingdom = "0.94";
$Australia = "1.49";
$United_Arab_Emirates = "1.42";

How can i do ?

  • 1. Your PHP file does not contain PHP code. That works, no problem, but once you have PHP code in there your `file_get_contents` code will not do what you probably expect. 2. That's HTML code, you need an HTML parser. [See here](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Imanuel May 03 '17 at 08:03
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Imanuel May 03 '17 at 08:03

1 Answers1

0

If you want to parse a HTML document you should use PHP's DOMDocument. Instead of file_get_contents() you should use loadHTMLFile(). Because you are using CSS-classes you should view this post on how to find elements via class.

Community
  • 1
  • 1
ahendwh2
  • 382
  • 2
  • 11