PHP gets external page content

I am getting html from another site from file_get_contens

, my question is how can I get a specific tag value?

let's say i have:

<div id="global"><p class="paragraph">1800</p></div>

      

how can i get the value of a paragraph? thanks

+2


a source to share


4 answers


If this example is really trivial, you can just use a regular expression. For general HTML parsing, PHP has DOM support:



$dom = new domDocument();
$dom->loadHTML("<div id=\"global\"><p class=\"paragraph\">1800</p></div>");
echo $dom->getElementsByTagName('p')->item(0)->nodeValue;

      

+4


a source


You need to parse HTML. There are several ways to do this, including using PHP's XML parsing functions.

However, if it is a simple value (as you said above), I would use the following simple code:

// your content
$contents='<div id="global"><p class="paragraph">1800</p></div>';

// define start and end position
$start='<div id="global"><p class="paragraph">';
$end='</p></div>';

// find the stuff
$contents=substr($contents,strpos($contents,$start)+strlen($start));
$contents=substr($contents,0,strpos($contents,$end));

// write output
echo $contents;

      



Good luck!

Christian Sciberras

(tested and working)

+3


a source


$input  = '<div id="global"><p class="paragraph">1800</p></div>';
$output = strip_tags($input);

      

0


a source


preg_match_all('#paragraph">(.*?)<#is', $input, $output);

print_r($output);

      

Unverified.

0


a source







All Articles